LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Links

Wiki
Blog

Forum Statistics

Users
Members:
1880
Members Online:
1
Guests Online:
103

Total Post History
Posts:
81452
Topics:
18717

7-Day Post History
New Posts:
1
New Topics:
1
Active Topics:
1

Our newest member
kwanail87

Other

FAQ
All times are UTC [ DST ]

Any Difference in Javascript Function Declarations?

Please wait...

Any Difference in Javascript Function Declarations?

Postby AlexCuse on Wed Mar 28, 2012 8:56 pm

When you need to define a javascript function in a page, you can do:

  1. function myFunc(param) {
  2.     //do stuff
  3. }


or you can do

  1. var myFunc= function (param) {
  2.     //do stuff
  3. };


The only difference I can think of is that the second can be modified on the fly, is there any other practical difference?
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031
LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031
LTD Gold - Rating: 1031
 
Posts: 5523
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: Any Difference in Javascript Function Declarations?

Postby tarwn on Wed Mar 28, 2012 9:39 pm

Hoisting also comes into play.

If you have two method declarations with the first syntax, then no matter where you execute (even prior to one of the definitions) you will get the value of the latest definition (these are evaluated prior to execution). Since the second method is assignment-based, it operates in the order you would expect, assigning new anonymous methods to the variable as it executes.

  1.  
  2. function TestA(){
  3.  
  4.    // return xyz(); would return 2
  5.  
  6.    function xyz(){
  7.       return 1;
  8.    }
  9.  
  10.    // return xyz(); would return 2
  11.  
  12.    function xyz(){
  13.       return 2;
  14.    }
  15.  
  16.    // return xyz(); would return 2
  17. }
  18.  
  19. function TestB(){
  20.    // return xyz(); error (xyz is not a function I think?)
  21.  
  22.    var xyz = function(){
  23.       return 1;
  24.    }
  25.  
  26.    // return xyz(); would return 1
  27.  
  28.    xyz = function(){
  29.       return 2;
  30.    }
  31.  
  32.    // return xyz(); would return 2
  33. }
  34.  

Um, yeah...I'm going to need you to come in on Saturday -- Bill Lumbergh, Office Space
User avatar
tarwn
LTD Admin
LTD Admin
LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897
LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897LTD Gold - Rating: 897
LTD Gold - Rating: 897
 
Posts: 3839
Joined: Fri Oct 12, 2007 11:10 am
Location: Raleigh, NC, USA

Re: Any Difference in Javascript Function Declarations?

Postby AlexCuse on Thu Mar 29, 2012 1:59 pm

I found this somewhat helpful: http://kangax.github.com/nfe/#expr-vs-decl

I think the section titled "Function Declarations" at http://ejohn.org/blog/javascript-as-a-first-language/ is probably a fair representation of how I feel about the difference.
Say what you like about the tenets of National Socialism Dude, at least it's an ethos
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031
LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031LTD Gold - Rating: 1031
LTD Gold - Rating: 1031
 
Posts: 5523
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated