First, take a look at the code
A = 2;
var A;
Console.log (a);
What will be the result, 2 or undefined.
The answer is 2.
Take another look at the code.
Console.log (a);
var a = 10;
What the result is.
The answer is undefined.
First, the code in JavaScript is executed from the top to the next line, which is not entirely true.
In fact, the engine compiles the JavaScript code before it interprets it, and all the declarations of variables and functions are found in the compilation, and they are associated with the appropriate scope.
Give an example of var a = 1; This statement can be divided into two parts
The first part is Var A; This sentence is in the compile-time phase
Part II A = 1; This sentence is executed in code order.
In general a statement of words in advance. (This process is called ascension)
The declaration itself will be elevated, and the assignment or other running logic will remain in place.
Look at this code later.
A = 2;
var A;
Console.log (a);
According to the declaration ahead of the above code equivalent to
var A;
A = 2;
Console.log (a);//The result is naturally 2
console.log (a);
var a = ten;
The above code is equivalent to
var A;
Console.log (a);
A = ten;
Because when I print, a just declares that there are no values, and naturally it's undefined.
OK, now that you know the reason for the variable declaration advance, look at the following code
Foo ();
function foo () {
console.log (a);
var a = 1;
}
According to the variable declaration ahead of us it is not difficult to see the result is undefined.
However, attentive students will find: Foo's call before the declaration. Why the function can be invoked before declaring it.
The whole function is in advance, because the statement in the function is a function declaration. Before I mistakenly thought only the function name advance, thought the head is big.
The function declaration is in advance, however, the function expression does not ... Nonsense not much to say, look at the code
Foo ();
var foo = function () {
Console.log ("1");
}
How it turned out. Error. Why. Look at the above sentence.
We know that the declarations of variables and functions are in advance, but they are prioritized.
The function is first promoted and then the variable.
How to understand, look at the following code
Foo ();
var foo;
function foo () {
Console.log ("1");
}
foo = function () {
Console.log ("2");
}
1,2,1,2,1 say your answer.
Since the function declaration has precedence over the variable, the above code is equivalent to the following
function foo () {
Console.log ("1");
}
var foo;//is equivalent to repeating declarations, ignoring.
foo (); The result is 1
foo = function () {
Console.log ("2");
}
On the scope of the problem, I believe we all have a certain understanding, then, look at the last piece of code
Foo ();
if (true) {
function foo () {Console.log ("1");}
} else{
function foo () {Console.log ("2");}
}
This is a spoiler. Although functions within else are not executed, the declaration will still be elevated.
Here left to everyone is said to be an interview problem, I wish you a happy to do the question
function Foo () {
getName = function () {alert (1);};
return this;
}
Foo.getname = function () {alert (2);};
Foo.prototype.getName = function () {alert (3);};
var getName = function () {alert (4);};
function GetName () {alert (5);}
Please write out the following output:
foo.getname ();
GetName ();
Foo (). GetName ();
GetName ();
New Foo.getname ();
New Foo (). GetName ();
New New Foo (). GetName ();