search

Home  >  Q&A  >  body text

javascript - Question about variable name promotion?

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?

巴扎黑巴扎黑2713 days ago575

reply all(5)I'll reply

  • 黄舟

    黄舟2017-06-12 09:34:55

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-12 09:34:55

    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.

    reply
    0
  • 代言

    代言2017-06-12 09:34:55

    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...

    reply
    0
  • 阿神

    阿神2017-06-12 09:34:55

    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

    reply
    0
  • 学习ing

    学习ing2017-06-12 09:34:55

    Panda Sang Zhengjie=_=

    reply
    0
  • Cancelreply