Pre-interpretation-basics

Source: Internet
Author: User

Pre-interpretation (variable promotion): In the current scope, the JS code from top to bottom before the browser will be the default first to all with the Var/function keyword in advance declaration or definition
In advance declaration with VAR variable (declare)
Pre-defined (declaration + definition) with function keyword (defined)
In the pre-interpretation phase, only advance declarations with the VAR keyword will be defined when JS is executed from top to bottom:
1 // Pre-interpretation stage: tells the browser that there is a variable named num in the current scope (window): Var num; 2 console.log (num); // ->undefined (only the default value that is not defined by the declaration is undefined)  var num = 13; // ->num=13; (definition: Assigning a value to a variable num=13) 4 console.log (num); // ->13

In the pre-interpretation phase, the Declaration + definition two parts of the function keyword are completed:
1 // Pre-interpretation phase: fn=xxxfff000 (xxxfff000 is a memory address) 2 console.log (FN); // the function itself functions fn () {Console.log ("OK");} 3 function fn () {4     console.log ("OK"5 }//6 Console.log (FN); // the function itself functions fn () {Console.log ("OK");}
The pre-interpretation occurs only in the current scope and starts with a pre-interpretation of the Var/function keyword under the window scope, and the code that appears in the function body with the Var/function keyword is a bunch of strings at this point (no real sense), So the beginning of the function in the body with these keywords do not need to tube
Only when the global code executes to function execution will a new private scope be formed, in the new private scope: The parameter of the function is assigned first, followed by the pre-interpretation in the private scope: the declaration or definition of the Var and the function keyword in the private scope in advance. Finally, the code in the private scope executes from top to bottom.

1 //The first is the pre-interpretation under Window scope: var n; var s; function fn=xxxfff000; 2Console.log (n,s);//n->undefined; s->undefined3 varn = 9;//n=9 under the global scope;4 vars = "str1";//s= "STR1" under the global scope;5 functionfn () {6Console.log (n,s);//n->undefined; (This is the FN private scope N) s->str1; (This is the global scope of s)7n = 7;//define the n=7 under the private scope;8s = "str2";//define the s= "STR2" under the global scope;9      varn = 6;//define the n=6 under the private scope;Ten } One fn (); AConsole.log (n,s);//at this point the output is global scope: n->9; s->str2;

Problem with the scope chain:
In a private scope, the JS code executes from top to bottom, and if a variable is encountered,
First look at whether it is your own private variables, if it is private, then we all the next operation (get the value, modify the value ...) ) are used by their own private, and there is no relationship outside
If it is not private, then go to the current scope of the previous scope of the lookup, if there is a previous level, then the next operation is in the operation of the upper scope of the variable; If the superior does not, then continue to look up, until the window is found
If you find window, not yet? If the variable = value is equivalent to adding a property name and a property value to the window: if the console.log (variable) Gets the value, the error will be

How to see if a variable is private:
First look at whether it is a formal parameter
Then see if it is declared in the private scope (see if there is no Var)
One of them is a private variable, and if two are not, it is not private and is searched according to the scope chain principle above.

Pre-interpretation-basics

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.