| Article Introduction: Dmitry Baranovskiy in his blog out of five test questions, testing whether people really understand JavaScript. |
Dmitry Baranovskiy in his blog out of five test questions, testing whether people really understand JavaScript. The topics are as follows
First question.
if (! () A "in Window") {
var a = 1;
}
alert (a);
Second question
var a = 1,
b = function A (x) {
X && A (--x);
};
alert (a);
Question number three.
function A (x) {
return x * 2;
}
var A;
alert (a);
Question fourth.
function b (x, Y, a) {
ARGUMENTS[2] = 10;
alert (a);
}
B (1, 2, 3);
Question fifth.
function A () {
alert (this);
}
A.call (NULL);
First question: Answer undefined
The JS function will first handle function declaration, formal parameter, variable declaration (create variable but not assign value, assign value in code execution phase) before executing. This example first handles the variable declaration before the code executes, that is, the parser first does a variable a, but not the function, and cannot assign a value to it. So with the variable A, note that the variable, the global variable is the same as the window's properties (in fact, different, such as the attribute can delete) so the "a" in window is true.
If this is changed to
if (! () A "in Window") {
A = 1;
}
Variable declarations will not be treated preferentially, and the results will be different.
As a contrast can be changed to the following, also according to the above instructions to answer, we try.
Question number two: Answer 1.
Just remember that the name of a named function expression is only valid within the defined function and is determined by the building rules of the ECMAScript scope chain.
Question number three: The answer is a function
As explained in the first question, the functions and declarations and variable declarations are processed first, so a is a function, and var A does not change the assignment, and since then there is no assignment, so a is always a function.
Question Fourth: Answer 10
JS each function has an implied arguments variable, is a class array structure, in turn, records the value of the parameter, and the parameter synchronization changes.
Question Fifth: Answer window
There is nothing to say, by default, window.
Are you all right?