views:

275

answers:

8

Possible Duplicate:
Javascript: var functionName = function() {} vs function functionName() {}

What's the difference between these two ways of declaring a function?

function someFunc() { ... }

var someFunc = function() { ... }

I'm not asking in the technical sense. I'm not asking which is better for readability, or which style is preferred.

+3  A: 

One advantage of using function someFunc() { ... } is that the function name appears in Firebug debugger. Functions that are declared the other way (var someFunc = function() { ... }) come up as anonymous.

Igor Zevaka
+1  A: 

Actually, the difference is that the second declaration gives us the ability to declare functions like this making it possible to have a function as a property for an object :

var myObject=new Object();
myObject.someFunc=function() { ... };
Soufiane Hassou
A: 

When you write

function Test() {
}

JavaScript is really creating a property to which it assigns the function object that once called will execute the code reported in the function definition. The property is attached to the object window, or to the object that contains the function definition.

kiamlaluno
You have some misconceptions here: first, there is a difference between named and anonymous functions in JavaScript, as you can see by calling `toString` on a function. Second, while it is true that a function declaration does add a property to the current varibale object, that's only the global object (aka `window` in browsers) in global code (as opposed to inside a function, for example). As a result, your third paragraph is completely false. (continued...)
Tim Down
... Third, there's a difference in when the function is assigned to the variable object. Functions created by a function declaration (e.g. `function test() {}`) may be used in code that appears before it, which is not true of functions declared by a function expression (e.g. `var test = function() {};`)
Tim Down
That is not what reported in "JavaScript: The Definitive Guide", which clearly states "When the JavaScript parser encounters a function definition, it parses and stores (without executing) that comprise the body of the function. The it defines a property (in the call object if the function definition is nested with another function; otherwise, in the global object) with the same name as the function tohold the function.
kiamlaluno
I removed the part that was not correct. The point is that in both the cases the JavaScript interpreter creates a property; technically, as asked from the OP there are not differences.
kiamlaluno
I'm not sure which of my points you disagree with. Which one is it? Regarding your answer as it is now, my first point still stands. It seems like my second point also still stands since a function declaration inside a function body does not become a property of the global object. My third point still stands because it points out a difference between function declarations and function expression that your answer doesn't mention.
Tim Down
Considering that what I report is done by the parser, I don't understand why what I report is not true. If the parser would allow to write "lambda x ( //... }", and would transform that in a normal function declaration, would that mean JavaScript supports lambda functions? Or if a language accept a line as "o = object[0, 23]" but the parser changes that in "o = array[0, 23]", does that mean the language supports objects?
kiamlaluno
I changed the answer to simply report what JavaScript parser does. What I meant is that you can write the function using the function syntax, but that is transformed in a property from the parser; in this case, from the user point of view (who was not asking difference from the technical point) there probably no differences. It would be like asking what is the difference between calling implode() and join() in PHP; there is no difference, one is alias of the other.
kiamlaluno
What the JavaScript parser should do (and indeed does do in most browsers) when it encounters function declarations or expressions is clearly defined in the ECMAScript Specification. The part that affects the user is that code can refer to a function whose declaration appears after it in the source. And it's still not true that a function declaration creates an anonymous function object: the function has a name, which appears in the output of the function's `toString()` method, and in some implementations (such as Mozilla browsers) is available as the `name` property of the function.
Tim Down
I didn't mean it creates an anonymous function. As per the properties the parser adds, it can easily add the property `name` to the function object it creates. Talking of Mozilla browsers, when you use Firebug, most of the functions are reported to be properties of an object (which includes also the object `window`).
kiamlaluno
+2  A: 

I am on different opinion with most of the people here. Technically this syntax may mean the same for declaring functions both ways (I stand incorrect on my last statement. I read up on a diff post why they are technically diff and I'll add in the end, why) ; but the way they play a role in evolving patterns is massive. I would highly recommend "Javascript: The Good Parts" by Doughlas Crockford.

But to prove my point in a subtle and a simple manner; here is a small example.

   //Global function existing to serve everyone
  function swearOutLoud(swearWord)
     {
      alert("You "+swearWord);   
     }
 //global functions' territory ends here

 //here is mr. spongebob. He is very passionate about his objects; but he's a bit rude.
 var spongeBob = {
  name : "squarePants",
  swear : function(swearWord) {
   name = "spongy";

   alert("You "+swearWord);
   return this;
  }  
 }

 //finally spongebob learns good manners too. EVOLUTION!
 spongeBob.apologize = function() {
  alert("Hey " + this.name + ", I'm sorry man"!");
  return this;
 }


 //Ask spongebob to swear and then apologize in one go (CASCADING EFFECT!!)
 alert(spongeBob.swear("twit").apologize());

if you look at the code above I declared a function with a name swearOutLoud. Which would take a swear word from any object or a call and will give you the output. It can do operations on any object using the "this" parameter that is passed to it and the arguments.

However second declaration is declared as an attribute of object called "spongeBob". This is important to note; as here I am moving towards an object driven behavior. While I am also maintaining "cascading effect" as I return "this" if i have nothing else to return.

Somthing similar is done in jquery; and this cascading pattern is important if you are trying to write a framework or something. You'll link it to Builder design pattern also.

But with functions declared as an attributes of an object I am able to achieve an object centric behavior which leads to a better programming paradigm. Unless designed well; individual functions declared outside with global access lead to a non-object oriented way of coding. I somehow prefer the latter.

To see cascading in effect, look at the last statement where you can ask spongebob to swear and apologize at once; even though spologize was added as an attribute later on.

I hope I make my point clear. The difference from a technical perspective may be small; but from design and code evolution perspective it's huge and makes a world of a difference.

But thats just me! Take it or leave it. :)

**EDIT So both the calls are technically different; because a named declaration is tied to global namespace and is defined at parse time. So can be called even before the function is declared.

 //success
 swearOutLoud("Damn");

 function  swearOutLoud(swearWord) {
    alert("You " + swearWord)
 }

Above code will work properly. But code below will not.

    swear("Damn!");
       var swear = function(swearWord) {
  console.log(swearWord);
 }
Priyank
The main part of this is unclear and doesn't have anything to do with the question.
Tim Down
+1  A: 

For readability, I'd say the first is clearly better. A future maintenance programmer, even assuming they're familiar enough with javascript to know many of the finer points coming up in this thread, are going to assume the first format.

For example, if they should some day want to ctrl-f to search for the definition of your function to see what's happening in there, are they going to first search for someFunc = function() or function someFunc()?

Also, to get downright typographical about it (since we're talking readablity) readers are often scanning the text quickly, and would be more inclined to skip over a line that starts with "var" if they're looking for a function definition.

I know this is a non-technical answer, but it's harder for humans to read code than computers.

Paul
A: 

Another difference is that, on most browsers, the latter allows you to define different implementations depending on circumstances, while the former won't. Say you wanted cross-browser event subscription. If you tried to define a addEventListenerTo function thusly:

if (document.addEventListener) {
    function addEventListenerTo(target, event, listener) {
        ....
    }
} else if (document.attachEvent) {
    function addEventListenerTo(target, event, listener) {
        ....
    }
} else {
    function addEventListenerTo(target, event, listener) {
        ....
    }
}

on some browsers, all the functions end up being parsed, with the last one taking precedence. Result: the above just doesn't work. Assigning anonymous functions to variables, however, will work. You can also apply functional and basic aspect oriented programming techniques using anonymous functions assigned to variables.

var fib = memoize(function (n) { 
    if (n < 0) return 0;
    if (n < 2) return 1;
    return fib(n-1) + fib(n-2); 
});

...
// patch the $ library function
if (...) {
    $ = around($, fixArg, fixResult);
}
outis
A: 

It is both true that the first form:

function test() { }

is a more recognized syntax and that the second form:

var test = function() { ... }

allows you to control the scope of the function (through the use of var; without it, it would be global anyway).

And you can even do both:

var test = function test() { ... test(); ... }

This allows you to define a recursive function in the second form.

EMK
I don't agree that the first form is a more recognized syntax and that the second form. They are just different forms, btoh of which are in common use. Also, named function expressions (which is what you're using in the last example) have odd behaviour in IE. See the following article: http://yura.thinkweb2.com/named-function-expressions/
Tim Down
+1  A: 

Style wise the second example is more consistent with other common ways to declare functions and therefore it could be argued that it is more readable

this.someFunc = function() { ... }
...
someFunc: function() { ... },

However, as also mentioned it's anonymous and therefore the name does not appear when profiling. Another way to declare the function is as follows which gets you the best of both worlds

var someFunc = function someFunc() { ... }
frglps