JavaScript scopes and closures

Source: Internet
Author: User

The following is for ES5

First, the variable promotion:

1, Global execution Context (script): variable definition, function declaration (before execution)

// undefined var a = 20;

The above code is equivalent to:

var // The variable is promoted to the top of the global execution context, and the default value is undefined // undefineda = 20;

Look at the function declaration again:

// ZS function FN (name) {    =;    Console.log (name, age);     var Age ;}

The above code is equivalent to:

function // function declaration overall promoted to the top of the global execution context    var age; // the variable is promoted to the top of    the function execution context age =;    Console.log (name, age);} FN (//ZS
Note: You still want to standardize the writing function. Do not recommend the above-mentioned wording, to first define after execution, increase readability. In addition, notice the difference between function declaration and function expression, function declaration can be executed before the definition, function expression can not, will be an error. Uncaught typeerror:fn1 is not a function

2, function execution context: variable definition, function declaration, this,arguments (before execution)

Second, JS scope: 1, no block-level scope
// no block-level scopes if (true) {    var name = ' Lily '//Lily

The above code is equivalent to the following code (recommended as follows):

var name; if (true) {    = ' Lily '//Lily
2, only function and global scope scope chain: When attempting to access a variable, the JS engine starts looking up from the current scope until the global scope stops, and the ordered collection of scopes becomes the scope chain. Three, this is a few different uses of the scene note: this to be executed in order to confirm the value, the definition can not confirm 1, as a constructor execution 2, as a normal function execution 3, as an object property execution 4,call,apply,bind Four, the closure is not finished to be continued ...

JavaScript scopes and closures

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.