Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Given this Javascript example I found

var sum = function() {
  var i, sum = 0;
  for(i = 0; i < arguments.length; i += 1) {
    sum += arguments[i];
  }
  return sum;
};

What advantage is there in assigning a function to a var?

share|improve this question

4 Answers 4

up vote 9 down vote accepted

The short answer I believe is simply that you are creating an anonymous function assigned to a variable, as opposed to creating a named function with...

    function sum() {}

A good way to check the differences is to call .ToString() on them and see the difference or you could do console.log(sum.name). One will give an actual name and the other nothing, namely the anonymous function (the one assigned to the var). There are specifics too, like the var sum = function(){} gets defined at run time and the function sum(){} gets defined at parse time.

share|improve this answer
    
What I mean by the last sentence is that if you tried to call sum() in a previous line above where it was defined by var, then there would be an error. If it is defined like in my answer then there would be no error, even if the call was above the function. –  harmonickey Sep 22 '12 at 23:55
7  
That explains the difference, but I don't think it explains the advantage. –  Keith Thompson Sep 23 '12 at 0:39
    
what's the difference between parse time and runtime? –  Ieyasu Sawada Sep 23 '12 at 12:52
1  
Doesn't have anything to do with named functions, you can still assign a named function to a variable –  Juan Mendes Apr 10 '13 at 17:48

I'm not a Javascript expert, so take this with a grain of salt. I think in some cases people might do it for style and same thing could be achieved by simply writing "function sum() {...}"

However, assigning a function to a variable is a very powerful technique in functional programming. If you are familiar with OOP, it is somewhat similar to polymorphism. Think of the classic example of Animal base class and Cat/Dog deriving classes. You can write code that works with Animal but when it calls a function, that function could do different work depending on the type of an instance.

In functional programming, you could have an algorithm which works with "a function" but if you are using a variable to call that function, you have the flexibility of assigning a different function at runtime.

For example, let's say you write an algorithm to present 10,000 data points in a window which is only 500 pixels across. Each pixel will then represent 20 data points and in order to present them you need to aggregate those 20 data points into a single value.

So let's say you define an algorithm to present the 10,000 points and this algorithm uses a function variable called aggregate like so:

...
displayValue = aggregate( numbersInOnePixel );
...

Now at runtime your user can select how the data is to be aggregated. Your actual function variable can be any one of the following:

aggregate = function sum() {...}
aggregate = function min() {...}
aggregate = function max() {...}
aggregate = function average() {...}
share|improve this answer

One advantage is that you can't use a function declaration in a block. They can only be at the top level of a file or directly within another function.

if (true) {
  function foo() {}
}
try {
  function foo(){}
}
switch (true) {
  default:
    function foo(){}
}

These are all unspecified by the standard and browsers do different things, see http://stackoverflow.com/questions/10069204/function-declarations-inside-if-else-statements. So if you have to use the other style sometimes, for consistency you might want to do so always

Also, I'm not sure of this one but if I remember correctly some old minifiers weren't smart enough to to handle function declarations and don't rename them.

share|improve this answer

Its mostly a matter of style, since the only situations where the difference shows up (calling some of the functions before ending all declarations; using the toString method) are kind of corner cases, in my oppinion.

One of the arguments I have heard supporting this var = style is that it is consistent with how you declare normal variables. This limits the number of language features you use and makes it simpler to indroduce your program to someone new to Javascript.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.