Use of the Apply method in JS
1, the object of inheritance, the general practice is to replicate: Object.extend
The prototype.js is implemented in the following ways:
Object.extend = function (destination, source) {
For (property in source) {
Destination[property] = Source[property];
}
return destination;
} |
In addition, there is a way, is: function.apply (of course, the use of Function.call is also possible)
The Apply method can hijack another object's method, inheriting another object's properties
Function.apply (Obj,args) method can receive two parameters
OBJ: This object will replace the This object in the function class
Args: This is an array, which is passed as a parameter to function (args-->arguments)
The Apply model code is as follows:
<script type= "Text/javascript" >functionPerson (name,age) {//define a class, human This. name=name;//name This. age=age;//Age This. sayhello=function() {alert ("Hello")}; } functionPrint () {//display the properties of a class This. Funcname= "Print"; This. show=function(){ varmsg=[]; for(varKeyinch This){ if(typeof( This[key])! = "function") {Msg.push ([key,":", This[Key]]. Join ("")); }} alert (Msg.join (" ")); }; } functionStudent (Name,age,grade,school) {//Student ClassPerson.apply ( This, arguments); Print.apply ( This, arguments); This. Grade=grade;//Grade This. School=school;//School } varp1=NewPerson ("Jake", 10); P1.sayhello (); vars1=NewStudent ("Tom", 13, 6, "Tsinghua Primary School")); S1.show (); S1.sayhello (); alert (s1.funcname); </script>
|
The student class would not have had any method, but after person.apply (this,arguments),
He has the SayHello method and all the attributes of the person class.
The show () method is automatically obtained after print.apply (this,arguments)
2, using the parameter array of apply to improve
Function.apply () Tips for improving program performance
Let's start with the Math.max () function, the Math.max can be followed by any parameter, and finally the maximum value in all parameters is returned.
Like what
Alert (Math.max (5,8))//8
Alert (Math.max (5,7,9,3,1,6))//9
But in many cases, we need to find the largest element in the array.
var arr=[5,7,9,1]
Alert (Math.max (ARR))//This is not possible. It must be written like this.
function Getmax (arr) {
var arrlen=arr.length;
for (Var i=0,ret=arr[0];i<arrlen;i++) {
Ret=math.max (Ret,arr[i]);
}
return ret;
}
It's troublesome and inefficient to write. If you use apply, look at the code:
function GetMax2 (arr) {
Return Math.max.apply (Null,arr);
}
The two pieces of code achieve the same goal, but GETMAX2 is elegant, efficient, and much simpler.
<script type= "Text/javascript" > // method apply the second parameter to an array var arr = [1,2,3,4,5 ]; // alert (Math.max (arr));//nan var max = Math.max.apply (null, arr); // 5 </script>
Another example is the push method of the array.
var arr1=[1,3,4];
var arr2=[3,4,5];
If we want to expand the arr2 and then add one to the arr1, and finally let arr1=[1,3,4,3,4,5]
Arr1.push (ARR2) is obviously not going to work. Because doing so will get [1,3,4,[3,4,5]]
We can only use a loop to go to one push (or Arr1.concat (ARR2), but the Concat method does not change arr1 itself)
var arrlen=arr2.length
for (Var i=0;i<arrlen;i++) {
Arr1.push (Arr2[i]);
}
It's been so easy since I got the Apply
Array.prototype.push.apply (ARR1,ARR2)
<script type= "Text/javascript" >//the multiple array push vara1 = [1,2,3,4,5]; vara2 = [6,7,8,9,10]; A1.push (A2); alert (A1); //[1,2,3,4,5,[6,7,8,9,10]];alert (a1.length);//length 6 vara3 = [1,2,3,4,5]; vara4 = [6,7,8,9,10]; [].push.apply (A3,A4); Alert (A3); //[1,2,3,4,5,6,7,8,9,10]alert (a3.length);//length Ten varA5 = [1,2,3,4,5]; varA6 = [6,7,8,9,10]; Array.prototype.push.apply (A5,A6); Alert (A5); //[1,2,3,4,5,6,7,8,9,10]alert (a5.length);//length Ten</script>