Js-call, apply

Source: Internet
Author: User

Here is mainly to do some of the predecessors of the summary, sometimes have their own views, first of all define the definition

1. Method definition

Call Method:
Syntax: Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])
Definition: Invokes one method of an object, replacing the current object with another object.
Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.
If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

Apply method:
Syntax: Apply ([Thisobj[,argarray]])
Definition: A method of applying an object that replaces the current object with another object.
Description
If Argarray is not a valid array or is not a arguments object, it will result in a TypeError.
If none of the Argarray and Thisobj parameters are provided, then the Global object is used as a thisobj and cannot be passed any parameters.

2, the same point and different points

1) The same point: the essence is the same, that is, two methods are to achieve a goal, look at the environment with which method is more appropriate;

Their first parameter can be directly understood as the context of the method (inheritance is also the same, after the inheritance will refer to), and then white, that is, when the method is executed, the method inside this point is the first parameter

2) different points: first the expression of the Second optional parameter, call () the second parameter is an object, can have many, and apply the second is an array, and at most only one.

Mainly on different occasions, choose the problem of that method. For illustrative purposes (both from the network):

A) More flexible parameters: alert (Math.max (5,7,9,3,1,6)); 9

Call Nothing to Change alert (Math.max.call (null,5,7,9,3,1,6)); 9

If you use apply, you can change the parameter into an array.

var arr = [5,7,9,3,1,6];

Alert (Math.max.apply (Null,arr));

Because arr is a parameter, you live, you can arbitrarily extend the length of the outside world, in exchange for call, how many parameters to write. Apply can also be used arguments, because the arguments itself array form (note that arguments is similar to the array, but the essence is not an array, but the nature of the array: arg[n],arg.length, etc.)

b) Apply according to the second parameter is the characteristics of the data set, you can do some array aspects of the application:

Array Push:

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]]

The stupid method is the loop to add, here can use Array.prototype.push.apply (ARR1,ARR2);

You can also use the Concat method

Array concat reduce Latitude:

var arr = [' A ', ' B ', [' C ', ' d '], ' e '];

Want to become a one-dimensional, this time can be used var Rtn_arr = arr.concat.apply ([],arr);//became [' A ', ' B ', ' C ', ' d ', ' e '];

3. Inheritance

 function Person (name,age,love) {
This.name=name;
This.age=age;
This.love=love;
This.say=function say () {
Alert ("Name:" +name);
}
}

Call mode
function Student (name,age) {
Person.call (This,name,age);
}

Apply method
function Teacher (name,love) {
Person.apply (This,[name,love]);
Person.apply (this,arguments); The same effect as the last sentence, arguments.
}

Obj.call (This) the method of calling call without methods is called inheritance, but essentially it is not out of the definition, where obj actually represents this construction method, illustrated by the above example:

Person.call (this,name,age), the person represents the constructor of the person (), which is actually the window. Person.call (this,name,age), the window object calls the person () method, and then the context (so-called this) points to sudent, this time the person () constructor method is actually student, This is why student has the attribute of person (so-called inheritance).

It is also possible to conclude that this inheritance is not a property of the prototype assignment, that is, the property that the person assigns by prototype, is not passed by this method.

Js-call, apply

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.