Use of the Apply method in JS detailed analytical _javascript tips

Source: Internet
Author: User
Tags extend

1, the object of inheritance, the general practice is to copy: Object.extend
Prototype.js is implemented in the following ways:

Copy Code code as follows:

Object.extend = function (destination, source) {
For (property in source) {
Destination[property] = Source[property];
}
return destination;
}

In addition, there is a way: function.apply (of course, the use of Function.call is also possible)

The Apply method can hijack another object's methods, 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 the function (args-->arguments)

The Apply demo code is as follows:

Copy Code code as follows:

<script>
function Person (name,age) {//Define a class, human
This.name=name; Name
This.age=age; Age
This.sayhello=function () {alert ("Hello")};
}
function Print () {//Show properties of Class
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", 13, 6, "Tsinghua Primary School");
S1.show ();
S1.sayhello ();
alert (s1.funcname);
</script>

The student class did not have 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 application of the parameters of the 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 argument, and finally return the maximum value in all parameters.

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.

Copy Code code as follows:

var arr=[5,7,9,1]
Alert (Math.max (ARR))//This is not the case. Be sure to write 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;
}


This is a cumbersome and inefficient way to write. If you apply it, look at the code:
Copy Code code as follows:

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.

Another example is the push method of an array.
var arr1=[1,3,4];
var arr2=[3,4,5];

If we want to expand the arr2, and then one by one append to the arr1, finally let arr1=[1,3,4,3,4,5]
Arr1.push (ARR2) is clearly not a good one. Because doing so will get [1,3,4,[3,4,5]]

We can only use one loop to go one push (of course we can also use Arr1.concat (ARR2), but concat method does not change arr1 itself)

Copy Code code as follows:

var arrlen=arr2.length
for (Var i=0;i<arrlen;i++) {
Arr1.push (Arr2[i]);
}

It's been so easy ever since the Apply
Array.prototype.push.apply (ARR1,ARR2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.