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?
|
Given this Javascript example I found
What advantage is there in assigning a function to a var? |
|||
|
|
|
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...
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. |
|||||||||||
|
|
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.
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. |
||||
|
|
|
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:
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:
|
|||
|
|
|
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 |
|||
|
|