The meaning and difference of call and apply

Source: Internet
Author: User

The difference is that the second argument of call can be any type, and the second argument of apply must be an array, such as Func.call (FUNC1,VAR1,VAR2,VAR3), corresponding to the Apply notation: func.apply (func1,[var1,var2 , Var3])

1. Call
Call method
Invokes one method of an object, replacing the current object with another object.

(such as Obj1.fun1.call (OBJ2,VAR1,VAR2)) means the object Obj1 method Fun1 placed on the object Obj2 to execute, the parameters are var1 and var2)
Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])
Parameters
Thisobj
Options are available. The object that will be used as the current object.
Arg1, Arg2,, ArgN
Options are available. The method parameter sequence will be passed.
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.
Simple sample (function call):

function Add (A, B)
{
alert (A+B);
}
function Sub (A, b)
{
Alert (A-B);
}
Add.call (sub,3,1);

The meaning of this example is to replace sub,add.call (sub,3,1) = = Add (3,1) with add, so the result is: alert (4); Examples of complex points (method invocations):

function Class1 ()
{
THIS.name = "Class1";
This.shownam = function ()
{
alert (this.name);
}
}
function Class2 ()
{
THIS.name = "Class2";
}
var C1 = new Class1 ();
var C2 = new Class2 ();
C1.showNam.call (C2);

Note that call means to put the C1 method on the C2, the original C2 is not Shownam () method, is now C1 the Shownam () method put to C2 to execute, so this.name should be class2, the result of execution is: alert (" Class2 ");
Implementing inheritance

function Class1 ()
{
This.showtxt = function (TXT)
{
alert (TXT);
}
}
function Class2 ()
{
Class1.call (this);
}
var C2 = new Class2 ();
C2.showtxt ("CC");

So Class2 inherit Class1, Class1.call (this) means to use the Class1 object instead of the This object, then Class2 all the properties and methods Class1, C2 object can directly call Class1 method to and attributes, the result of execution is: alert ("CC");
Right, that's it, that's how JavaScript simulates inheritance in object-oriented, and can also implement multiple inheritance.
Multiple inheritance

function Class10 ()
{
This.showsub = function (A, B)
{
Alert (A-B);
}
}
function Class11 ()
{
This.showadd = function (A, B)
{
alert (A+B);
}
}
function Class2 ()
{
Class10.call (this);
Class11.call (this);
}

It's easy to implement multiple inheritance with two call
Of course, JS inheritance There are other methods, such as the use of prototype chain, this is not part of the scope of this article, just here to explain the use of call
Said call, and of course, apply, these two methods are basically a meaning

The difference is that the second parameter of call can be any type, and the second argument of apply must be an array

2. Apply
Both apply and call are the same in effect, but they differ in parameters.
The meaning for the first parameter is the same, but for the second argument:
Apply passes in an array of parameters, which is the combination of multiple parameters into an array, and call is passed in as a call parameter (starting with the second argument).
such as Func.call (FUNC1,VAR1,VAR2,VAR3) corresponding to the wording of the Apply: Func.apply (Func1,[var1,var2,var3])
Skill (Code is elegant and its execution is highly efficient)

Alert (Math.max (5,8))//8
Alert (Math.max (5,7,9,3,1,6))//9
var arr=[5,7,9,1]
Alert (Math.max.apply (Null,arr));

The meaning and difference of call and 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.