Eight-point summary of JavaScript scope chain _javascript tips

Source: Internet
Author: User
Tags closure

1. The scope chain of JavaScript functions is defined as the scope chain and the run-time scope chain;

2. When the function is defined, it has an attribute [[scope]] that identifies its defined scope chain, and defines the scope chain [[scope]] to comply with the rule that the scope chain [[scope]] is always the scope chain of the execution of the external function where the function is defined;

3. The definition scope chain of global functions contains only the properties of Windows;

4. When a function is executed, the scope chain always presses the head of the scope chain into the current active object (it contains this,arguments, parameters, local variables) when defined;

5. When the function executes, the variable addressing is always looked down from the top of the scope chain, so the global variable is addressed at the slowest rate;

6. When the internal function is executed, he still has access to its complete scope chain. This is why closures can be accessed at run time by variables that have been defined by external functions that have been terminated;

7. When a function execution encounters a with statement, it temporarily presses all the properties of the specified object at the top of the scope chain as the top of the scope chain;

8. When a function execution encounters a catch, it temporarily presses the catch-specified Error object at the top of the scope chain as the topmost of the scope chain;

Here's an example and plot the scope chain to deepen the understanding:

There is this piece of code:

Copy Code code as follows:

function assignevents () {
var id = "xdi9592";
document.getElementById ("Save-btn"). onclick = function (event) {
SaveDocument (ID);
};
}

The anonymous closure generated by this function is called closure, and the scope chain is plotted when the following diagram defines the scope chain and closure for assignevent execution:

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.