Course Note: Pre-interpretation in--javascript

Source: Internet
Author: User

1, Pre-interpretation (variable promotion): In the current scope, before the JS code execution, the browser will first put all the Var and function keyword in advance declaration or definition
var num = 12;
Statement (DECLARE): Var num; --no assignment at the time of the declaration, our default value is undefined
Definition (defined): num=12;

2, with Var and the pre-interpretation with function is not the same:
var: When pre-explained, only the undefined is declared (the assignment definition is completed only when the code executes)
Function: At the time of the pre-interpretation, the declaration and the definition are completed together (the contemporary code is no longer in control when it executes to the definition)

Pre-interpretation under global scope: Var num; fn=xxxfff000; (The FN function is also a bunch of strings at this point, so the global pre-interpretation, regardless of the function, and only when the function executes, the inside of the string into the code when the inside of the pre-interpretation)
Console.log (num);-->undefined
var num = 12;-->num=12
The function fn () {21~25 is all part of the FN definition, but it is done in the pre-interpretation, so we skip directly.
Console.log (num);-->undefned
var num = 13;
Console.log (num);-->13
}
FN ();
Console.log (num);-->12


3, with Var and without var difference?
Console.log (num);-->undefined
var num = 12;
Console.log (num);-->12

Console.log (num);-->uncaught Referenceerror:num is the not defined error is the variable does not exist (JS error mechanism: The above code to execute an error (browser exception information), The following code is not being executed.)
num = 12;
Console.log (num);

The following differences are only useful for global variables under global scope
var num=12; -On the one hand, a global variable is defined, and the other is equivalent to adding a property num to Window
num=12; --the equivalent of adding a property to Window num-->window.num=12;
var num = 12;
Console.log (num);-->12

num = 12;
Console.log (num);-->12


4. About variables in private scopes
Console.log (num);-->undefined
var num = 12;
function fn () {
Console.log (num);--> outputs the value of the NUM variable under global scope 12
num = 13;--> Changes the value of the NUM variable to 13 globally
Console.log (num);--> outputs the value of the NUM variable under global scope 13
}
FN ();
Console.log (num);--> outputs the value of the NUM variable under global scope 13


function fn () {
Console.log (num);-->uncaught referenceerror:num is not defined
num = 13;
Console.log (num);
}
FN ();
Console.log (num);


function fn () {
num = 13;-->num is not a private variable, then it looks up one level, finds no global, and adds a num property name under Window with a property value of 13
Console.log (num);-->window.num 13
}
FN ();
Console.log (num);-->window.num 13


Summarize:
1. The pre-interpretation only occurs under the current scope
2, how to distinguish the private variables?
Variables declared in a private scope (functions), parameters, and only variables that are private in both cases--only variables with Var in the private scope or parameters that are private
In a function, I encountered a variable, according to the above-mentioned rules to see if it is private, if it is private, then the current function in the body of this variable and outside no relationship, is a different variable
3, in a private scope to find a non-private variable, we go to its upper-level scope to find, if it is superior, then use the superior variable, if the superior also did not, then continue to look up ... Until you find the window.
4, function execution form a private scope, to protect the private variables inside the outside interference, we put the function of this protection mechanism-"closure"

Course Note: Pre-interpretation in--javascript

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.