JavaScript functions have two main features, one is the function is an object, the other is the function provides scope support, and today the main chat function scope support.
Simply look at two concepts:
named function expression
var add = function Add (a) { return a }
This code describes a function called the ' named function expression '.
If you omit a name from a function expression, it is an ' anonymous function expression ', which is an anonymous function, such as
var add = function (A, b) { return a + b;};
Finally look at the ' function declaration '
function foo () { //body}
A function declaration can only appear in "program code", that is, in another function body or globally. This definition cannot be assigned to a variable or property, nor can it be used as a parameter to a function call.
Global scope
function foo () {}
Local scope
function local () { function bar () {} return bar;}
At this point the bar is within the local, that is, outside the access, called the local scope, so we see this code
var a = 1;function aa () { console.log (a); }
At this point the result of the output is 1, well understood, because inside the function is not accessible, so will go outside to find, until found
Add a line of code, and then look at
var a = 1;function () { console.log (a); var a =2; }
At this point the output is underfined; it is surprising that there is no definition before the function can be accessed outside the functions, the definition is not accessible, what is the actual situation,JS defined inside the function of the variable will be promoted to the top of the function body, but only the definition, not assigned value, so the above code is like this
var a = 1;function () { var A; Console.log (a); var a =2; }
Since a is defined but not assigned, it certainly underfined after the console.
Now that the function declaration is lifted, will the function expression be, look at the following example
Global functions function foo () { alert (' Global foo ');} function Bar () { alert (' Global Bar ');} function Hoistme () { console.log (typeof foo); Console.log (typeof bar); Foo (); Bar (); function foo () { alert (' local foo '); } var bar = function () { alert (' local bar ');} ;} Hoistme ();
After the last code, this time it is easier to understand that the function inside will overwrite the function defined outside, but what is the result?
Console.log (typeof foo); "Function" Console.log (typeof bar);//"Undefined" foo ();//"local foo" bar ();//Typeerror:bar is not a function
Isn't it a little bit surprising that it's easy to figure it out
As I said earlier, the function declaration is ahead of time, so the variable foo and its definition implementations are advanced, and the function definition implementation is independent of the writing order, and this is demonstrated by the intentional writing in the back.
But the function expression does not have this skill, so the variable is advanced, but the definition is not in advance.
Thank you for watching
On function scope and declaration of JS in advance