Closure scope chain, chain prototype

A, closure

 

Closure:

Closure means the function has access to another domain variable function effects, the most common way to create a closure is to create a function within another function, local variables of this function by another function, the use of closures can break scope. (That is, the ability to read a function of internal variables of other functions)

 

Characteristics closure package:

Then nested functions within a function;

Internal parameters and variables can be referenced function outer layer;

Parameters and variables will not be garbage collection mechanism.

 

Using closures designed primarily for private methods and variables. Closure of the advantages : to avoid polluting the global variables, the disadvantage is the closure will be permanent memory, increases memory usage, improper use is likely to cause a memory leak. In JS, i.e. closure function, the function will produce only act on the concept.

 

Closure use (3): Third, package and private attributes of the object private methods; first, a function of the internal variable can be read; second, to make these variables remain in memory.

Benefits: enables encapsulation and caching;

Disadvantages: consume memory, improper use can cause memory overflow problems.

 

Using closures Precautions:

Since the closure will make the function of the variables are stored in memory, memory consumption is large, it can not be abused closure, otherwise it will cause performance problems webpage in IE may lead to memory leaks. The solution is before exiting the function will delete all local variables are not used.

 

 

Second, understanding the scope chain

The role of the scope chain: ensure the implementation of environment variables and functions have access to is ordered, the scope chain variable can only access up to the window object variable access has been terminated, the scope chain variable access is not down Allowed. Simply put, the scope is accessible range of variables and functions that control the visibility and the scope of the life cycle of variables and functions.

 

Three, JavaScript prototype, the prototype chain of understanding

Each object in its internal initialization of a property, that is, prototype (prototype), when we access an object's properties, if the internal object this property does not exist, then it will go to prototype where to find this property, this prototype will has its own prototype, so this has been looking down, is what we usually say the concept prototype chain.

 

关系:instance.constructor.prototype=instance.__prototype__

 

Features: JavaScript objects are passed by reference, each new object we created entity and not have a copy of their own prototypes. When we modify the prototype, it will inherit the object associated with this change. When we need a property, JavaScript engine will look at whether the current object has this attribute, if not, will find out whether his prototype object with this property, so recursive down, built-in objects have been retrieved Object.

Guess you like

Origin www.cnblogs.com/p0123/p/12001690.html