JavaScript Core Concept Learning

Source: Internet
Author: User

Execution context and scope chain

The execution context (referred to as the context) determines what variables, functions, and data can be obtained during JS execution, a program may be split into many different contexts, each of which binds a variable object, like a container, which is used to store all defined or accessible variables, functions, etc. in the current context. The context at the top or outermost is called the global context, and the global context is represented in the browser by the window proxy, which is global in Nodejs.

It should be noted that the context and scope (scope) is a different concept, JS itself is single-threaded, whenever a function is executed, it will produce a new context, the context will be pressed into the context stack of JS, function execution End is ejected, So the JS interpreter is always executed at the top of the stack, and when a new context is generated, the variable object of that context is first bound, including arguments and the variables defined in the function, and then the scope chain that belongs to that context is created. Finally, assign this to the object to which this function belongs.

When a new context is generated when the function is executed, the variable object of the current context is bound first, and then the scope chain is created, and we know that the definition of function can be nested within the context created by other functions, or in the same context, and can be defined side by side. A scope chain is essentially a bottom-up string of variable objects bound by all nested defined contexts, so that nested functions can nest variables in the upper context, while the parallel function does not interfere with each other.

Closed Package

If you understand the mechanism of the context and scope chain mentioned above, it is clear that the concept of closure is that each function creates a new context and scope chain at the time of invocation, and the scope chain is to concatenate the variable objects bound by the outer context. Enables the current function to get the variables, data, and so on for the outer context. If we define a new function in function and return the inner function as a value, the scope contained within the inner function will be returned together, even if the inner function is executed in another context. Its internal chain of scopes still retains the original data, and the current context may not be able to get the data in the native outer function, using the scope inside the function to form a closure.

We know that in JS everything is the object (Objet), but JS does not have a class, JS is based on the prototype, but not all objects have prototype this property:

var a = {};console.log (a.prototype); // undefined var function  // + = {}var c = "Hello"//undefined

JavaScript Core Concept Learning

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.