Js variable in a recent study to enhance something

1 variable lift:

That is, the variable var statement, its statement will be promoted to the top of the current scope (note the variable declaration to enhance the assignment of variables does not improve)

for instance:

 <script>
    console.log(a);//undefined
    var a = 10;
    function test(){
      console.log(a);//undefined
      var a =12;
      console.log(a);//12
    }
    test();
    console.log(a);//10
  </script>

The above code is equivalent to:

<script>
    var a;
    console.log(a);//undefined
    a = 10;
    function test(){
      var a;
      console.log(a);//undefined
      a =12;
      console.log(a);//12
    }
    test();
    console.log(a);//10
  </script>

2 So whether if the let and declare const variables will increase? let and const are block-level scope

Tested with the following example:

 <script>
    console.log(a);//undefined
    var a = 10;
    function test(){
      console.log(a);//报错
      let a = 12;//or const a = 12;
      console.log(a);//12
    }
    test();
    console.log(a);//10
  </script>
View Code

It concluded: let and const, does not apply to variable lift; let const and with the concept of 'temporary dead zone', that is from the beginning to this part of the scope of variables declared, you can not use the variable, otherwise it will error.

Statement 3 function improvement:

 Function declaration in three ways:

1 function declaration:

function test(){};

2 function expression:

was test = function () {};

3 with the Function constructor: // // in es6 replaced by subsequent supplementary class

var test = new Function();

Where the function declaration and the constructor will be lifted:

The following examples demonstrate:

<Script> 
   Test (); // Output: function declaration 
    var Test = function () { 
      the console.log ( "function expression" ); 
    } 
    Test (); // Output: function expression 
    function Test () { 
      Console. log ( "function declaration" ); 
    } 
    Test (); // output: functional expression 
    var Test = new new the Test (); 
    test.say (); // output: constructor 
    function the Test () {
       the this ... SAY = function () { 
        the console.log ( "constructor" ); 
      } 
    }
</script>

4 function declaration is higher than the variable declarations:

as follows:

<Script> 
    the console.log (Test); // Output: Test ƒ () { 
    //    the console.log ( "stated function"); 
    // } 
    function Test () { 
      the console.log ( "stated function" ); 
    } 
    var Test =. 1; 
</ Script>

Note: personal records, if the wrong place, welcome to point out.

Guess you like

Origin www.cnblogs.com/Zxq-zn/p/11441435.html