This article mainly introduces five small examples that can help you understand the core closures and scopes of JavaScript. This article is an article translated from abroad, for more information, see five small scripts to help you understand the closure and scope of the JavaScript core. Before running on the console, try to answer what will pop up in each case. Then you can create a test file to check your answer. Are you ready?
1,
The Code is as follows:
If (! ("A" in window )){
Var a = 1;
}
Alert ();
2,
The Code is as follows:
Var a = 1,
B = function a (x ){
X & a (-- x );
};
Alert ();
3,
The Code is as follows:
Function a (x ){
Return x * 2;
}
Var;
Alert ();
4,
The Code is as follows:
Function B (x, y, ){
Arguments [2] = 10;
Alert ();
}
B (1, 2, 3 );
5,
The Code is as follows:
Function (){
Alert (this );
}
A. call (null );
My predictions are: undefined, 1, unknown, 10, null
The answer is at the end of this Article. before reading the answer, do you dare to leave your guesses?
Correct answer: 1. undefined 2, 1 3, function a (x) {return x * 2} 4, 10 5, [object window]