javascript - Question about variable name promotion?
巴扎黑
巴扎黑 2017-06-12 09:32:55
0
5
598

After the following code is executed, it will be undefined

<script type="text/javascript">
if (! 'a' in window) {
  var a = 123;
}
console.log(a);
</script>

Explanation 'a' in window is true, but when was a declared? Please tell me, thank you!

I changed the code again:

<script type="text/javascript">
if (! 'a' in window) {
  a = 123;
}
console.log(a);
</script>

Resulta is not defined. Since a has been declared, why is this error reported?

巴扎黑
巴扎黑

reply all(5)
黄舟

Let’s talk about variable improvement first

if (! 'a' in window) {
  var a = 123;
}
console.log(a);

No matter whether this code enters the code block of if, a still exists

Because when JavaScript is executed, all variables and declarative functions defined through var will be promoted to the top of the current scope

Variables created through var do not have block-level scope, so they will be promoted to the top of the current function scope

Variables defined in the global scope are all attributes of window

So this code is actually executed in this order

var a;
if (! 'a' in window) {
  a = 123;
}
console.log(a); // undefined 

defines a, but does not assign a value, so it naturally outputs undefined

About ! 'a' in window is actually to execute non on the 'a' string first, and get false, there is no window.false attribute in window, and false is returned , without entering the if code block.

You can try the following examples

false in window; // false

window.false = 123;

false in window; // true

!false in window; // false

true in window; // false

window.true = 456;

!false in window; // true

'true' in window; // true

Second question

if (! 'a' in window) {
  a = 123;
}
console.log(a); // Uncaught ReferenceError: a is not defined

After understanding the above, it is very simple. It is useless to define var (there is no promotion), and if is not entered, resulting in a not being defined and an error being reported.

大家讲道理

There is nothing wrong with this execution, ! 'a' in windowThis is false, and then the assignment of a is not executed, and then the console is undefined.
If you want the assignment to be executed, just change the judgment condition to !('a' in window).

If you still don’t understand, check the operator precedence list.

代言

varVariable promotion will occur when declaring. During the editing phase, the code declaration is placed at the beginning of the function or code, so it becomes like this:

<script type="text/javascript">
var a;
if (! ('a' in window)) {
  a = 123;
}
console.log(a);
</script>

So a in window is true.

As for

<script type="text/javascript">
if (! ('a' in window)) {
  a = 123;
}
console.log(a);
</script>

The code does not contain var a. So there is no problem of variable promotion, so a has not been declared, so! ('a' in window) is true, so a is 123

https://developer.mozilla.org...

阿神

You misunderstood the first paragraph
'a' in window is false
!'a' in window Only when it is true
will it be executedvar a = 123;
Only when a is assigned the value 123 Existence

学习ing

Panda Sang Zhengjie=_=

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template