JS-apply、call、bind

Recently I viewed a lot of articles about apply, it is readily record it.

Apply

apply: another object method can hijack method, another object of the inherited properties.

Function.apply (obj, args) method capable of receiving two parameters, then obj Function hijacked properties and methods of 
obj: Function object class will replace this object 
args: This is an array, it will be passed as a parameter Function (args -> arguments The)

Call

call: the meaning and apply the same parameter list is just not the same.

The Function.call (obj, [the param1 [, param2 [, ... [, paramN]]]]) 
obj: Function object class will replace this object 
params: This is a parameter list

Example:

<Script type = "text / JavaScript">  
     / * define a human * /   
    function the Person (name, Age)   
    {   
        the this .name = name;  
         the this .age = Age;   
    }   
    / * define a student class * /   
    function Student (name , Age, Grade)   
    {   
        Person.apply ( the this , arguments);  
         the this .grade = Grade;   
    }   
    // Create a class of students   
    var student = new new student ( "zhangsan", 21 is, "a year" );  
     // test  
    console.log ( "name:" + student.name + "\ the n-" + "Age:" + student.age + "\ the n-" + "Grade:" + student.grade);  
     // you can see the test results name: zhangsan age: 21 grade: Grade   
    // students not assigned to a class which I attribute name and age, ah, why is the existence of these two value of the property does, is apply the magic.   
</ Script>

分析: Person.apply(this,arguments);

this: you create objects that represent a student at this time

arguments: an array, i.e. [ "zhangsan", "21", "Grade"]; 

Popular thing about that is: to perform with student content inside the Person class, this.name the like exist in the Person class statement inside, this property will be created inside the object to a student

2. call example

You may be modified to apply the following functions in Studen inside:

Person.call(this,name,age);

So ok

3. Under what circumstances apply, under what circumstances call

In the case of a target parameter, if the formal parameter is the time of the array, such as apply sample which passed parameter arguments, this parameter is an array type, and when calling Person parameter list is corresponding to the same (i.e. Person and Student's list of the top two parameters are the same) can be used to apply, if the parameter list my Person is such that (age, name), and the Student parameter list is (name, age, grade), so that you can use the call is achieved, i.e. corresponding to a position directly specified parameter value list (Person.call (this, age, name, grade));

4. apply some other clever uses

Observant people may have noticed, when I call to apply the method, the first parameter is the object (this), the second parameter is an array of collections, when calling Person, he needs is not an array, but why he gave me an array that I can still be an array resolved to a one parameter, this is a clever use apply, and can be an array of default converted to a list of parameters ([param1, param2, param3] converted to param1, param2 , param3) If we use this program to achieve each item in the array, be installed for the parameter list, you may have to charge a kung fu, apply the aid of this feature, so there is a less efficient method :

 

a) Math.max array may be implemented to obtain a maximum

Because Math.max parameters which are not supported Math.max ([param1, param2]) is an array

But it supports Math.max (param1, param2, param3 ...), it is possible to solve var max according to the characteristics of just apply = Math.max.apply (null, array), so you can easily get an array of a maximum items (a number of assembling Apply will change to a parameter by one parameter passed to the method)

         In this time of the call the first argument to a null, this is because the object is not to call this method, I only use this method to help my operation, and the results returned on the line, so direct passed a null in the past

b) Math.min obtain the minimum can be realized in an array

And max is a likewise thought var min = Math.min.apply (null, array);

c) Array.prototype.push two arrays may be implemented merged

The same push method does not provide an array of push, but it provides a push (param1, param, ... paramN) so the same can also apply to pack a change in this array, namely:

[javascript]  view plain  copy
 
  1. vararr1=new Array("1","2","3");  
  2.   
  3. vararr2=new Array("4","5","6");  
  4.   
  5. Array.prototype.push.apply(arr1,arr2);  

 

Can also be understood, of arr1 called push method, the parameters are assembled by the number of changed to apply the parameter list set.

Usually under what circumstances, you can use special usage similar Math.min the like apply:

In general the objective function requires only a list of n arguments, without receiving in the form of an array ([param1 [, param2 [, ... [, paramN]]]]), can solve this problem by way of subtly apply!

5. Summary:

At first I do not know of very apply, the last multi-read several times, knocking several times more than their own codes, they would understand the middle of the truth, so, no matter what the situation, as long as they are willing to move the brain, are willing to get knocked code such a technology would grasp ...   

The fourth part was there such content, ingenious solution to a real problem, this is certainly not a beginner can think of solutions (this is not my own thought), does not have some knowledge of programming will not think of this, or word, accumulate more, study more, to enhance their capacity and ability to understand programming ideas is the most critical!

 

Article Source: https://www.cnblogs.com/chenhuichao/p/8493095.html

Guess you like

Origin www.cnblogs.com/yangchin9/p/10960189.html