We can write functions in 2 ways:
var v1 = m1('hello'); // error
var v2 = m2('hello'); // OK
var m1 = function(param){
// ...
return param;
}
function m2(param){
// ...
return param;
}
var v1 = m1('hello'); // OK
var v2 = m2('hello'); // OK
As I know there is only one difference - time of creation:
m2
in compilation time, so we can use it 'before' declaration - like in my case.
m1
in assignment time (code goes line by line) and we cannot use it before.
Is one more efficient for memory or performance?
In which case is one way more semantic, in which case second?
Is here next difference?
When we should use first and when second one?
.
// edit really primitive perf test - same results
// http://jsperf.com/performance-function-writing-way
var m1 = function(param){ // ... return param; }
it may make more sense when you consider that a function is an object, and we're just assigning a name to the object. Think of it as saying var m1=[1,2,3]; The content of functions declared this way is also compiled.
When assigning a function like this, we are not restricted to an unnamed function, and I can refer to the function by the name or by the variable.
for more info you must look into this article...