The difference between JS's apply () and call ()

Source: Internet
Author: User

1. Different syntax of the respective corresponding:

1 /* apply () method */ 2 function.apply (thisobj[, Argarray]) 3 4 /* Call () method */ 5 Function.call (thisobj[, arg1[, arg2[, [,... ArgN]]]);

2. Their different definitions:

Call Method:
Syntax: Call (Thisobj,object)
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.

(in the same place):

All "can be used instead of another object to invoke a method that changes the object context of a function from the initial context to the new object specified by Thisobj."

(difference):

Apply: There can be at most two parameters-the new this object and an array Argarray. If you pass multiple arguments to the method, the arguments are written into the array, and of course, even if there is only one argument, it is written into the array. If Argarray is not a valid array or 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.

Call: It can accept multiple parameters, the first parameter is the same as apply, followed by a list of parameters. This method is mainly used when the JS object method calls each other, so that the current this instance pointer is consistent, or in special cases need to change the this pointer. If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

As you can see, the function of apply and call is the same, except that the parameter list is passed in a different form.

code example:

1 functionAnimal (name) {2      This. Name =name;3      This. ShowName =function() {4Console.log ( This. Name);5     };6 }7 8 functionCat (name) {9Animal.call ( This, name);Ten } OneCat.prototype =NewAnimal (); A  - functionDog (name) { -Animal.apply ( This, name); the } -Dog.prototype =NewAnimal (); -  - varCat =NewCat ("Black cat");//Call must be an object +  - varDog =NewDog (["Black Dog"]);//apply must be an array +  A cat.showname (); atDog.showname ();

Some other clever uses of apply

  (1) Math.max can be implemented to get the largest of the array:

Because Math.max does not support Math.max ([PARAM1,PARAM2]), which is an array, it supports Math.max (param1,param2 ...), so you can solve Var max= according to the characteristics of apply. Math.max.apply (Null,array), so that it is easy to get the largest item in an array (apply will convert an array to a parameter to a parameter to pass to the method) this block is called when the first argument to NULL,    This is because there is no object to call this method, I just need to use this method to help me to calculate the return of the results on the line, so directly passed a null past. This method can also be implemented to get the smallest item in the array: Math.min.apply (Null,array) (2) Array.prototype.push can implement merging of two arrays the same push method does not provide an array of push,          But it provides push (Param1,param2...paramn), and can also use apply to convert the array, namely: 1.var arr1=new Array ("1", "2", "3");          2.var arr2=new Array ("4", "5", "6");    3.array.prototype.push.apply (ARR1,ARR2); The length of the merged array is obtained, because push is the length of the returned array, which is also understood, ARR1 calls the push method, and the parameter is converted to the collection of argument lists by apply

The difference between JS's apply () and call ()

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.