Home > database >  The little knowledge
The little knowledge

Time:09-29

A, what is a closure?

Closure is access to other variables in the function scope of function

1. The principle of closure:

In my own words: when we define a function, need to access the function of internal variables cannot be access, so need to define an anonymous function within the function and the return to go out, so that it can function in access to

2. The scope chain

First, the background of the execution environment has a variable - the variable object, said global environment variable object always exist, and function within the local environment variable object, then only exist in the execution of a function

In the create function, will first create a object contains global variable scope chain (pointer to the variable object), the scope chain stored within the scope attribute

Call this function, can create an execution environment for this function, and then by copying function within the scope attribute to construct objects in the execution environment of the scope chain

In the function execution environment scope front to create an event object (0 active objects, a global object)

Inside the function definition of the anonymous function external functions of active objects will be added to his scope

When internal function returns, the anonymous functions within the scope of the chain is initialized to contain the activities of the outer function object and global variables, so internal anonymous functions can access external function of all variables

Note that
After the external function, activity object is not destroyed, because the scope chain internal function in reference to the active object
When the outer function is returned, his scope chain will be destroyed, but he still for active objects to be stored in the memory, until the internal anonymous function is destroyed, the activities of the outer function object will be destroyed,

3. Create a common way of closure

Inside a function to create an anonymous function

The function out () {var num=1; {return return function (n) (n + num)}} var. A=out () the console log (a) (1) the console. The log (out () (2)

4. This point to problems in closure

This is based on the function at runtime execution environment binding

In the global function, this is equal to the window

When the function is called the method of an object, this object is equal to the

Each function in the call will automatically obtain two special variables: this and the arguments, the internal function in the search when the two variables, will only search until its active objects, it is impossible to access to the external function of the two variables

The solution
Keep the external function scope of this object in a closure can access to the variable, can make the closure to access the object,

Var name="welkin" var obj={name: "zhangsan", getName: function () {return function () {return this. The name}}} to the console. The log (obj. The getName () ())//welkin


Use variables to store this:

Var name="welkin" var obj={name: "zhangsan", getName: function () {var that=this; Return the function () {return that. Name}}} the console. The log (obj) getName () ())
Three, how to implement inheritance

1. The basic thoughts of implementation inheritance

We use the implementation inheritance in js (inherit the actual method) based on the prototype chain inherited
Using the prototype to a reference type inherits the other reference types of properties and methods

2. The prototype chain

Every constructor has a prototype object (a prototype object has a pointer to a constructor)

Each instance has a pointer to the interior of the prototype object

The prototype object is equal to another type of instance

At this time of the prototype object will contain a pointer to another prototype, another prototype also contains a pointer to another constructor,

3. The prototype system

If all objects are private fields (prototype), is the object's prototype

No read attributes, if the object itself, can continue to access the object's prototype, until the prototype is empty or find

4. Operation of prototype method

Object. The create to create a new Object, based on the specified prototype prototype can be null

Object. GetPrototypeOf get an Object's prototype

Object. SetPrototypeOf set an Object's prototype

5. The default prototype (array method is which come of?)

All inherited object reference type default
All the functions of the default prototype is the object instance, so the default prototype contains an internal pointer, pointing to the object. The porototype
This is all the custom types will inherit the toString () the valueOf () the root cause of the



  • Related