Look at the following code:
The code copy is as follows:
if (!("aa" in window)) {
alert('oh my god');
var aa = 1;
}
alert("aa" in window);
alert(aa);
Answer the following questions:
Will an error be reported? How many times will it pop up?
Is the second alert true or false?
What pops up in the third alert?
Why?
Think about it, and then test it. If you answer correctly, you don’t need to read the following articles.
-----------------------------
Defining variables in JS is too simple. You can just use a var, or even without var:
The code copy is as follows:
var a = 1;
Here a is the variable name and 1 is the variable value. Alas, this is too basic. Look at the following code:
The code copy is as follows:
var a;
alert(a);
In the firebug test, undefined will pop up. This is a very familiar string, which seems to indicate that the variable is undefined. But I think I've already vared it, this is the definition, but there is no value attached.
Let's do something that is really undefined:
The code copy is as follows:
alert(a);
That's right, just alert a variable that has never appeared at all. What will happen?
Firebug directly reported an error: a is not defined. It means that a is not defined. Combining the previous code, this is confusing. What is the difference between this undefined and the previous undefined?
In fact, the previous code is equivalent to this:
The code copy is as follows:
var a = undefined;
alert(a);
In other words, when a variable is declared without assigning a value, JS will pass an undefined value to the variable. Note that this is a "value", which means that a already has a value, and this value is called "undefined".
The variables behind the direct alert have never appeared, which means that this is the real undefined.
Simply put: there is no variable without a value in JS, and the value is assigned when the variable is declared.
Then we look at the following code:
The code copy is as follows:
alert(a);
var a = 1;
Will this code report an error? Because when alert is alert, the variable a has not yet appeared.
But there is no error in this way, but an undefined value pops up. It indicates that the variable a already exists, but the value is not what we want, but is undefined. What is the problem?
Because the var variable declaration is the same as the function declaration, it will be advanced, in fact, the above code looks like this:
The code copy is as follows:
var a;
alert(a);
a = 1;
This way you will understand.
Therefore, the key to this problem is: the var declaration will reach the top of the scope in advance, but the attached value will not be - a very tangled setting, I don't know why I did this. I personally think this is a flaw of JS.
There is a code habit now that advocates putting variable declarations in front of the scope, probably considering this - even if you don't write them in front, JS will go ahead in advance.
Now let me release the answer to the question at the beginning of the article:
Only two alerts will pop up, and the alert in if will not be executed, because the var declaration is advanced, the real code looks like this:
The code copy is as follows:
var aa;
if (!("aa" in window)) {
alert('oh my god');
aa = 1;
}
alert("aa" in window);
alert(aa);
Although aa is empty, it will be true when judged by 'aa' in window, because a does exist and the value is undefined. So if code won't execute. I won't talk about the last two alerts.
Personally, I feel that this is a very nonsensical question. We should understand his reasons, but despise him as a trap.
The above question is also the reason why I wrote this article. I saw this code from an online article, but there is no answer in it. I couldn't beat up my sister, so I went to stackoverflow and asked to find out. The answer is this article.
But this is a very basic question, actually! ! !
Haha, forgive me, there is another question:
The code copy is as follows:
var b = {}
alert(b.aa);
alert(b.aa.bb);
This is also a way to declare variables. So, will this code report an error? Why?