What is closure, apply and call methods

What is closure?

Closure to read other functions is a function of internal variables. In JS, only the internal function subroutine to read local variables, the closure can be understood as simply "in a defined function within the function."

Fn function () { 
var. 1 = B; // closure
function box () {// the console.log (B);}
// return a function that is a closure
return box;
}
// RES box is function
var res = fn ();
after 2 seconds // res function call, or to print the value of b, the closure allows local variables permanent memory
setTimeout (function () {res ( );}, 2000);

  

Features closure package

1: a function of internal variables can be read. Scope of a variable is nothing more than two types: global and local variables. JS language so special, is that internal function Global variables can be read directly. On the other hand, you can not read the local external function within function variables

2: Let the values ​​of these variables are always stored in memory.

Application scenario closure package

1: Function as a return value.

2: The function is passed as a parameter to the callback function is essentially inherited constructor in the subclass, call the parent class constructor, so that subclasses have attributes and methods of the parent class

call / apply method

and all are designed to apply a function to change the running context, i.e. context exists, in other words, to change the function of the interior points of this.

Both exactly the same effect, but not the same way that accepts arguments.

fn.call(obj, arg1, arg2 [, argN]);

The method call is a parameter the second parameter value is assigned the this method call to the class of the first argument in the method call Function class methods (i.e., methods) appearing sequentially assigned to the class (i.e., method) accepted

Note: calling the function call or apply method is equivalent to calling the function

function box (A, B) { 

the console.log ( the this , A, B) 

} 

var arr = [1111 ]; 

// use arr change box in the this 

box.call (arr, l, 3)

 

fn,.apply(obj, [arg1, arg2,…, argN]);

apply an array of transfer parameters, call in the form of a list method takes two parameters apply

A, the first parameter and the second parameter of the method call is the same as that assigned to the class (i.e., method) appearing in this

B, the second parameter is an array type, each element of this array are sequentially assigned to the class (i.e., method) accepted parameters

function box (A, B) { 

the console.log ( the this , A, B) 

} 

var ARR = [1111 ]; 

// the apply change box in the this 

box.apply (ARR, [ 2,4])

 

Using math apply using methods min and max

var arr1 = [1,2,3,4,5,6,6,45,45];

console.log(Math.min.apply(null,arr1));

console.log(Math.max.apply(null,arr1));

 

rototype prototype

Most object-oriented programming language, are "class" (class) is based on object inheritance. JavaScript language is not the case, its object inherits a "prototype object" (prototype) basis.

JavaScript generates a new object in the constructor, and therefore can be regarded as a constructor template object. Properties and methods of an object instance, the function may be defined in the internal structure.

By the object instance constructors are defined attribute, although very convenient, but there is a drawback.

Between multiple instances of the same constructor can not be shared properties, resulting in a waste of system resources. cat1 cat2 and are two instances of the same constructor, they all have meow method. Since meow in each instance is to generate the object above, the two examples is generated twice. That is, each create a new instance, it will create a new meow method. This is not only unnecessary, but also a waste of system resources, because all meow methods are the same behavior, should be fully shared.

 

prototype concept

Each constructor has a prototype property, this property when generating instances, become the prototype object instance of the object.

Each object inherits another object of JavaScript, which is known as the "prototype" (prototype) object.

On the one hand, any object can act as prototype of other objects; on the other hand, since the prototype objects are objects, so it also has its own prototype. null can also serve as a prototype, except that it does not own prototype object.

function Animal(name) { 

this.name = name;
}
Animal.prototype.color = 'white';

var cat1 = new Animal('大毛');

var cat2 = new Animal('二毛');

cat1.color // 'white'

cat2.color // 'white'

 

In the above code, the constructor Animal prototype object, the object is an instance cat1 cat2 and the prototype object. Add a color property on the prototype object, the result, instance object inherits the property.

Properties of the prototype object instance is not a property of the object itself. But as long as the modified prototype object, change will be immediately reflected in all instances of objects.

Role of the prototype object, is the definition of the properties and methods of all instances of the shared objects.

This is also referred to as prototype object it causes, and examples of the object properties and methods may be viewed as sub-objects of the object prototype chain derived from the prototype object, it is possible to define itself, it is also possible to define the prototype object. Since the prototype itself is an object, it has its own prototype, so the formation of a prototype chain (prototype chain).

For example, the object A is the prototype object b, b c objects prototype object, and so on.

The role of the prototype chain "is that when reading an attribute of an object, JavaScript engine to find the properties of the object itself, if not, go on to its prototype, if you still can not find it to prototype to prototype Find.

If until the topmost Object.prototype still not found, returns undefined.

For example, if you make the prototype property of a function points to an array, it means that the function can be used as a constructor of the array, because it generates instance objects can be invoked by the prototype property array method

var MyArray = function () { }; //

MyArray.prototype = new Array();

var mine = new MyArray();

mine.push(1, 2, 3);

mine.length //

 

The above code, the constructor mine MyArray instance objects, since the prototype property MyArray example points to an array, such an array can call mine method (These methods define the prototype object instances in the array above).

Prototype chain is the use of this principle will inherit a prototype constructor points to another instance of an object constructor to implement inheritance.

Box function () { 

// Box configured = this.name 'Lee'; 

} 

function Desk () { 

// Desk configured 

the this .age = 100 ; 

} 

Desk.prototype = new new Box (); // Asc inherited Box, by prototype, forming a chain 

var Desk = new new Desk (); 

Alert (desk.age); 

Alert (desk.name); // get an inherited property

 

Inherited prototype chain hybrid combination constructor inheritance

function Box (Age) { 

the this .name = 'Lee' the this .age = Age; 
} 
Box.prototype.run = function () { return the this .name + the this .age; 
}; function Desk (Age) { 
Box.call ( the this , age); // constructor inherited inherited property name and age 
} 
Desk.prototype = new new Box (); // prototype inheritance chain run method var Desk = new new Desk (100 ); 
Alert (desk.run () );





 








 

Guess you like

Origin www.cnblogs.com/shubo168/p/11769173.html