This article gives a detailed summary of the use of the apply method in js. If you need it, you can refer to it and hope to help you.
1. The general method of Object Inheritance is copying: Object. extend
The implementation of prototype. js is as follows:
The Code is as follows:
Object. extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}
In addition, there is another way: Function. apply (of course, Function. call is also acceptable)
The apply method can hijack another object and inherit the attributes of another object.
The Function. apply (obj, args) method can receive two parameters.
Obj:This object will replace this object in the Function class
Args:This is an array, which will be passed as a parameter to Function (args --> arguments)
The apply sample code is as follows:
The Code is as follows:
Script
Function Person (name, age) {// defines a class, human
This. name = name; // name
This. age = age; // age
This. sayhello = function () {alert ("hello ")};
}
Function Print () {// display class attributes
This. funcName = "Print ";
This. show = function (){
Var msg = [];
For (var key in this ){
If (typeof (this [key])! = "Function "){
Msg. push ([key, ":", this [key]. join (""));
}
}
Alert (msg. join (""));
};
}
Function Student (name, age, grade, school) {// Student Class
Person. apply (this, arguments );
Print. apply (this, arguments );
This. grade = grade; // grade
This. school = school; // school
}
Var p1 = new Person ("jake", 10 );
P1.sayhello ();
Var s1 = new Student ("tom", "Tsinghua Elementary School ");
S1.show ();
S1.sayhello ();
Alert (s1.funcName );
Script
Student classes do not have any methods, but after Person. apply (this, arguments,
It has the sayhello method and all attributes of the Person class.
After Print. apply (this, arguments), the show () method is automatically obtained.
2. Use Apply parameter array to improve
Function. apply () Skills in improving Program Performance
Let's start with the Math. max () function. Math. max can be followed by any parameter and return the maximum value among all parameters.
For example
Alert (Math. max (5, 8) // 8
Alert (Math. max (,) // 9
However, in many cases, we need to find the largest element in the array.
The Code is as follows:
Var arr = [5, 7, 9, 1]
Alert (Math. max (arr) // This is not acceptable. Be sure to write it like this
Function getMax (arr ){
Var arrLen = arr. length;
For (var I = 0, ret = arr [0]; I ret = Math. max (ret, arr [I]);
}
Return ret;
}
This is troublesome and inefficient. If you use apply, check the Code:
The Code is as follows:
Function getMax2 (arr ){
Return Math. max. apply (null, arr );
}
The two pieces of code achieve the same purpose, but getMax2 is elegant, efficient, and concise.
Another example is the array push method.
Var arr1 = [1, 3, 4];
Var arr2 = [3, 4, 5];
If we want to expand arr2, append it to arr1 one by one, and finally let arr1 = [1, 3, 4, 5]
Arr1.push (arr2) obviously does not work. In this case, [, 4, [, 5] is obtained.
We can only use one loop to push one by one (of course, arr1.concat (arr2) can also be used, but the concat method does not change the arr1 itself)
The Code is as follows:
Var arrLen = arr2.length
For (var I = 0; I arr1.push (arr2 [I]);
}
With Apply, things have become so simple.
Array. prototype. push. apply (arr1, arr2)