JavaScript learning the difference between call and apply _ javascript skills

Source: Internet
Author: User
The apply and call functions are the same, but they have different parameters. 1. call
Call Method
Call a method of one object to replace the current object with another object.
Call ([thisObj [, arg1 [, arg2 [, [,. argN])
Parameters
ThisObj
Optional. Will be used as the object of the current object.
Arg1, arg2, and argN
Optional. The method parameter sequence will be passed.
Description
The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by thisObj.
If the thisObj parameter is not provided, the Global object is used as thisObj.
Simple column child (function call ):

The Code is as follows:


Function add (a, B)
{
Alert (a + B );
}
Function sub (a, B)
{
Alert (a-B );
}
Add. call (sub, 3, 1 );


In this example, we use add to replace sub and add. call (sub, 3, 1) = add (3, 1), so the running result is: alert (4); example of a complex vertex (method call ):

The Code is as follows:


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: call means to put the c1 Method on c2 for execution. Originally, c2 does not have the showNam () method. Now, it is to put the showNam () method of c1 to c2 for execution, so this. the name should be class2, And the execution result is: alert ("class2 ");
Implement inheritance

The Code is as follows:


Function Class1 ()
{
This. showTxt = function (txt)
{
Alert (txt );
}
}
Function Class2 ()
{
Class1.call (this );
}
Var c2 = new Class2 ();
C2.showTxt ("cc ");


In this way, Class2 inherits Class1. Class1.call (this) means that the Class1 object is used to replace this object. Then, does Class2 have all the attributes and methods of Class1, the c2 object can directly call the Class1 Method and Its Attributes. The execution result is: alert ("cc ");
That's right. This is how javaScript simulates inheritance in object-oriented systems, and implements multiple inheritance.

Multi-Inheritance

The Code is as follows:


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 multi-inheritance with two calls.
Of course, there are other methods of js inheritance, such as using prototype chain. This is not part of the scope of this article, but only describes the call usage here.
When we talk about call and apply, these two methods basically mean one thing.
The difference is that the second parameter of call can be of any type, and the second parameter of apply must be an array.

2. apply

The apply and call functions are the same, but they have different parameters.
The meaning of the first parameter is the same, but for the second parameter:
The apply parameter is an array of parameters, that is, multiple parameters are combined into an array and the call parameter is passed in (starting with the second parameter ).
For example, the apply method for func. call (func1, var1, var2, var3) is: func. apply (func1, [var1, var2, var3]).

Skills (Code elegance and high execution efficiency)

The Code is as follows:


Alert (Math. max (5, 8) // 8
Alert (Math. max (,) // 9

Var arr = [5, 7, 9, 1]
Alert (Math. max. apply (null, arr ));

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.