"If (! (" "A" "in window)) {var a = 1;} alert (a); why the result is undefined"

Since all variables are global attribute window, so the statement var a = 1; equivalent to window.a = 1;
All variables are declared at the top end of the scope
alert("a" in window);
var a;

Although this statement is after the alert, but the alert pop-up is still true, because the JavaScript engine will first scan all variable declarations, and then these variable declarations to the top of the final effect of the code is this:

var a;
alert("a" in window);
So for this question, we can be understood as: variable declaration is ahead, but there is no variable assignment, because this line includes a variable declaration and variable assignment, you can split the statement following code:
var a;    //声明
a = 1;    //初始化赋值

When the variable declaration and assignment together with, JavaScript engine will automatically divide it into two in order to declare variables in advance, will not step assignment because he is likely to affect the implementation of the Code unexpected early results

So, knowing these concepts later, we look back at the title of the code, in fact, it is equivalent to:
var a;
if (!("a" in window)) {
    a = 1;
}
alert(a);

So the title means: first a statement, and then determine whether there is a, if there is no value of 1, it is clear that there is always a window where the assignment statement is never executed, so the result is undefined

Guess you like

Origin www.cnblogs.com/yujiao-99/p/12656389.html