Here are 5 small scripts that help you really understand the JavaScript core-closures and scopes. Before the console runs, try to answer what pops up in each case, and then you can create a test file to check your answers. Are you ready?
1,
Copy Code code as follows:
if (! () A "in Window") {
var a = 1;
}
alert (a);
2,
Copy Code code as follows:
var a = 1,
b = function A (x) {
X && A (--x);
};
alert (a);
3,
Copy Code code as follows:
function A (x) {
return x * 2;
}
var A;
alert (a);
4,
Copy Code code as follows:
function b (x, Y, a) {
ARGUMENTS[2] = 10;
alert (a);
}
B (1, 2, 3);
5,
Copy Code code as follows:
function A () {
alert (this);
}
A.call (NULL);
My prediction answers were: Undefined, 1, unknown, 10, null.
The answer is at the end of this article, before you look at the answer, do you dare to leave your guess?
Correct answer: 1, undefined 2, 1 3, function A (x) {return x * 2} 4, 10 5, [object window]