Js enhance the function and variable in the upgrade

Variable lift and enhance the function :

   It is to declare a variable or function to enhance the entire code section to the beginning of the current scope (global scope or function scope).

JavaScript function field in the range of the minimum domain; "{}" is not scope for loops, while loops, if statements, switch statement.

A variable lift:
    variables var declared in advance to the starting position of the current scope, is declared. In situ assignment.

1 form referred to: var a = "lift"; (Keywords are "var")

2. enhance manner: "var a", to the top of the current scope, a = "lift", defined position in the prototype.

3. Specific codes:

eg:

 1. console.log( a)  ----> undefined
   There are a = 10;
      console.log(a)  ---->   10
 2.console.log(a);
  var a = "I am the global variable";
  console.log(a);
  function fn(){
    console.log(interiorA);
    var interiorA = "I was variable within the function";
    console.log(interiorA)
  }
  fn();
      ↓↓↓↓↓↓↓↓
  Variable enhance execution order
  var a; // variable lift, this time just a statement, not assignment
  console.log(a);    // undefined
  a = "I am the global variable" // assignment at this time only
  console.log (a); // I is a global variable in
 
  function fn() {
    var interiorA; // variable lift, the scope of the range of the function
    console.log(interiorA);    //undefined
    interiorA = "I was variable within the function" // local variable at this time of the assignment
    console.log (interiorA) // I was variable within the function
  }

 II. Functions lift

   1. The function of the form referred to lift: function fn () {......} (function declaration must be in the form, not in the form of function expression). 

   2. The function of lifting manner: "function fn () {......}" whole function declaration code block to the top of the current scope,

      Original location to the absence of the code.

   3. All function using the function statement, will enhance the overall scope statement as long as the current number

      Can be used in any location, type an assignment to create a function to enhance the statement is not.

      When the function and the same name as a variable, the variable lift higher, to enhance the function of the following variables

      Cause the value of the function to take effect

    There;

    function a(){}

    console.log(a);

    a = 10;

eg:  

  function fn(){

 

     console.log(a);

 

      var a = "hello";

 

      console,log(a);

 

      function a(){1}

 

      console.log(a);

 

      a = "world";

 

      console.log(a);

 

      function a(){2}

 

      cosole.log(a)

 

  }

 

  After lift equivalent to the formula  

   function fn(){

 

      var a; variable maximum lift;

 

      function a () {1}; function of the following variables to upgrade;

 

      function a(){2};

 

      console.log(a);       f2

 

     a = "hello"

 

      console.log(a);       hello

 

      console.log(a);       hello

 

      a = "world"

 

      console.log(a)        world

 

      console.log(a)        world

 

  }

   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/SYJ1205/p/11922416.html