Identifier function is often said that the function name in a function declaration can not be omitted, and the function expression can be omitted.
As we all know, javascript engine will be treated as a variable name function name, so the use of command when the function Statement function, the entire function will be the same as the variable lift was promoted to the head of the code. So the first implementation function declaration also does not complain, but will function expression.
For the following code
foo();
bar();
var foo=function bar(){
console.log(1);
}
According to our theory variables and functions to enhance the promotion can certainly be an error, the actual run
foo(); //Uncaught TypeError: foo is not a function
bar(); //Uncaught ReferenceError: bar is not defined
My understanding is the beginning, the variable foo is raised, equivalent to
var foo //undefined
foo=function bar(){
console.log(1);
}
Browsers do not know what the bar is being given ReferenceError: defined normal. So it follows:
var foo=function bar(){
console.log(1);
}
bar();
or
new function bar(){//作为New表达式(NewExpression)的一部分,它也是函数表达式
console.log(1);
}
bar();
The result is still running bar (); // Uncaught ReferenceError: bar is not defined. This and imagination does not match ah, not to perform, why bar is still defined? Since the function identifier is treated as a variable name, then the identifier for the function expression, how could not find, so where its scope in?
So with the following code:
var foo = function bar(){
console.log(bar);
}
foo();
Print results are as follows:
ƒ bar(){
console.log(bar);
}
Sure enough ideas right, scope identifier of the function expression of its own function in the body . Function name can not call a function expression, if you want to call, and only in the case of recursion.
In addition, to find such eggs on the identifier function expression by using a search engine, examples are as follows:
var f1 = function b1(){
return f2();
}
var f2 = function b2(){
return f3();
}
var f3 = function b3(){
debugger;
}
f1();
When each function gave the identifier, the call stack will show the name of the function to be called
Is not it can be relatively bright √