javascript-Basic Concept Summary

Source: Internet
Author: User
Tags closure

"Scope"
The scope in JavaScript can be understood as the size of a statement's environment, global scope, function scope, and eval scope. There is no block-level scope in JS.
When it comes to scopes, we have to talk about the execution environment, and the execution environment is the most important concept in JS. The execution environment defines the other data that variables or functions have access to, and determines their behavior. Each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object. In a Web browser, the global environment is considered a Window object, and all the code in an execution environment is destroyed by the environment, and all the variables and function definitions stored therein are destroyed.

Each function has its own execution environment. When the execution flow enters a function, the environment of the function is pushed into an environment stack. After the function executes, the stack pops up its environment and returns control to the previous execution environment.

When code executes in an environment, a scope chain of variable objects is created. The purpose of a scope chain is to ensure an orderly access to all variables and functions that the execution environment has access to. The front end of a scope chain is always a variable object of the environment in which the code is currently executing. The next variable object in the scope chain comes from the containing (external) environment, where the next variable object comes from the next containment environment, which continues to the global execution environment, and the variable object of the global execution environment is always the last object in the scope chain.

Note that variables defined in a local scope can be used interactively in both the global and local contexts. The internal environment can access all external environments through the scope chain, but the external environment does not have access to any variables and functions in the internal environment. Each environment can search up the scope chain to query for variables and function names, but no environment can enter another execution environment by searching down the scope chain.

A scope chain is essentially a list of pointers to variable objects, and he references but does not contain variable objects.

"Closures"
A closure is a function that has access to a variable in another function scope, where it is separated from the anonymous function (anonymous function: Creates a function and assigns it to a variable, in which case the function is called an anonymous function, and the Name property of the anonymous function is an empty string). A common way to create closures is to create another function inside one function. A closure is an object that holds the entire variable.

Closure function: During function execution, to read and write the value of the variable, you need to find the variable in the scope chain, when the flexible and convenient closure comes in handy, we know that when a function is called, it creates an execution environment and the corresponding scope chain, The closure then gets the variables and elements that the developer wants, along the scope chain.

Note: In Wikipedia, this defines closures: closures (also known as lexical closures or function closures) refer to a reference to a function or function, bound together with a reference environment. This reference environment is a table that stores each non-local variable (also called a free variable) of the function. --closures are different from general functions, which allow a function to access a non-local variable when it is called outside of an immediate lexical domain.

Closures are flexible and easy to implement, so that they can be accessed only through specific methods of the object. However, unreasonable use of closures can result in wasted space, memory leaks, and performance consumption.

"Prototype and prototype chain"
Speaking of the prototype chain, we have to talk about the prototype pattern for creating objects: we create each function with a prototype (prototype) attribute, which is a pointer to an object, and the purpose of this object is to include properties and methods that can be shared by all instances of a particular instance. In the literal sense, then prototype is the prototype object of the object instance created by invoking the constructor. The advantage of using a prototype object is that you can have all object instances share the properties and methods that it contains. We can access the values saved in the prototype through the object instance, but we cannot override the values in the prototype through the object instance. If we add a property in the instance that has the same name as the prototype instance, we create the property in the instance that will mask the properties in the prototype.

Whenever a new function is created, a prototype (prototype) attribute is created for the function based on a specific set of rules, which points to the prototype object of the function.

Prototype chain: Used to implement the implementation of JS in the inheritance. (Many languages support inheritance in two ways: interface inheritance and implementation inheritance.) Interface inheritance inherits only the method signature, while implementing inheritance inherits the actual method. Because the function does not have a signature, implementation inheritance can only be implemented in Ecmascriot.

The basic idea of the prototype chain as the main method to implement inheritance is to use the prototype to inherit another reference property and method from a reference type. Learn about the relationships of constructors, prototypes, and instances: each constructor is a prototype object, and the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object. If we let the prototype object be equal to another instance of the type, then the prototype object will contain a pointer to another prototype, and the corresponding other prototype contains a pointer to another constructor. The progression of such layers constitutes the chain of examples and prototypes. This is the concept of the prototype chain.

When a function is called, the variables and elements that need to be found and obtained are obtained through a layer-by-step search of the prototype chain mechanism on the prototype object or the inherited object.

When the function is created, there is scope, when called, there is a scope chain, when it is inherited there is a prototype chain, when you need to get the scope chain or the prototype chain of variables or values, there is a closure.

javascript-Basic Concept Summary

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.