JavaScript variable declaration and scope promotion

Source: Internet
Author: User

Knowledge of the past always forget, encountered code and can not understand. You need to review it and record it.

1   Add (+); 2   function Add (b) {3       alert (A +b); 4   }

The code can output 3, why is it not executed sequentially? Should be//add is not defined ...

JavaScript's variable declarations have a hoisting mechanism, and the JavaScript engine, when executed, promotes the declaration of all variables to the front of the current scope.

That is, function add (A, b) {alert (A+B)} is a functional declaration that tells us that there is a function that is promoted to the front of the scope. Similarly, the declaration of other variables is also promoted.

The above example is equal to:

1   function Add (b) {2       alert (A +b); 3   }4   Add (+);

Note: The expression of a variable assignment or function is not promoted except that the declaration is promoted.

1 // undefined 2 alert (b);  // undefined 3 alert (c);  // undefined 4 5 var a=0,b=1,c=2;

It is equivalent to:

1 var A,b,c 2 alert (a); 3 alert (b); 4 alert (c); 5 a=0; 6 b=1; 7 c=2;

The function is the same: only the VAR=ADD is promoted, and the subsequent expressions are still in place.

1  (function() {2  //Add is not a function3  var  function(b) {4     alert (A +b)5  }  6  }) ()

Note: Even if you name the expression, it will not be promoted

1   (function() {2   Add ();  // add is not a function 3   var function Add (b) {4     alert (A +b)5   }6   }) ( )

Scope:

1 var scope= "Global";   2 function T () {  3     console.log (scope);   // ubdefined 4     var scope= "Local"  5     console.log (scope);   // Local 6 }  7 t ();  

var scope= "global";  is the global scope, in the function, also defines a local variable, the variable promotion. The assignment is not lifted, so it's undefined, and the next assignment is local.

In C + + and other languages, there is a block-level scope for all of the {}, but JS does not have a function scope

When you want to invoke an object, it will go from the local to the global, one after the other, and the search will not continue.

1 var name= "Global";   2 if (true) {  3     var name= "local";   4     Console.log (name)    //local5}  6 console.log (name );    // Local

If is not a function, there is no block-level scope, and its context is global.

Equivalent:

1   var name= "Global";   2   var name= "local";   3   if (true) {  4        console.log (name)  5   }  6   console.log (name);  

Look at this, from the local to the global, one to find, found that will not continue to find.

1Name= "Lwy"; 2 functionT () {3     varName= "Tlwy"; 4     functions () {5         varName= "Slwy"; 6 Console.log (name); 7     }  8     functionSS () {9 Console.log (name); Ten     }   OneS ();//Slwy ASS ();//Tlwy - }   -T ();

S () first in the local search, found the var name= ' Slwy ' on the output.

SS () in their own internal no, look up, in the T () function, find the var name= ' Tlwy ', also output.

If you do not find it, you will continue to find it in the global. This form a chain of scope, in the most internal is the chain head, not found along the chain to find, chain end is the global scope.

A common problem, button, is to pop the last one every time:

1 var aLi = document.getelementsbytagname (' li '); 2  for (var i = 0; i < ali.length; i++) {3   function () {4     // the last value pops up each time 5   }6 }

When the page is finished loading, this JS function runs, when we click, it will find the value of I, the most internal anonymous function is not, go up, the outer layer found I, is 4, the output of 4.

To resolve this method:

 1  for  ( var  i = 0; i < ali.length; I++ 2  Ali[i].onclick = function   (msg) { 3  Show:function   () { 4   alert (msg)  5  }  6  return Show;  7  }) (i)  } 
1  for (var i = 0; i < ali.length; i++) {2  (function(i) {3    function  () {4      alert (i) ; 5      }; 6 }) (i)7 }

is to save them all.

The article is used to study records, if there are errors, to correct.

JavaScript variable declaration and scope promotion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.