The example analyzes the call () and apply () methods in javascript, and the example analyzes the apply

Source: Internet
Author: User

The example analyzes the call () and apply () methods in javascript, and the example analyzes the apply

1. method definition

Call method:
Syntax: call ([thisObj [, arg1 [, arg2 [, [,. argN])
Definition: call a method of an object to replace the current object with another object.
Note:
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.

Apply method:
Syntax: apply ([thisObj [, argArray])
Definition: apply a method of an object and replace the current object with another object.
Note:
If argArray is not a valid array or an arguments object, a TypeError occurs.
If neither argArray nor thisObj is provided, the Global object will be used as thisObj and cannot be passed with any parameters.

2. Common instances

A,

Copy codeThe 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); // Note: Functions in js are actually objects, the Function name is a reference to the Function object.

B,

Copy codeThe Code is as follows:
Function Animal (){
This. name = "Animal ";
This. showName = function (){
Alert (this. name );
}
}
Function Cat (){
This. name = "Cat ";
}
Var animal = new Animal ();
Var cat = new Cat ();
// Use the call or apply method to send the showName () method of the original Animal object to the cat object.
// The input result is "Cat"
Animal. showName. call (cat ,",");
// Animal. showName. apply (cat, []);

Call means to put the animal Method on cat for execution. In the past, cat didn't have the showName () method. Now it is to put the showName () method of animal on cat for execution, so this. the name should be Cat

C. Implement inheritance

Copy codeThe Code is as follows:
Function Animal (name ){
This. name = name;
This. showName = function (){
Alert (this. name );
}
}
Function Cat (name ){
Animal. call (this, name );
}
Var cat = new Cat ("Black Cat ");
Cat. showName ();

Animal. call (this) means that the Animal object is used to replace this object, so does Cat have all the attributes and methods of Animal, the Cat object can directly call the Animal method and attributes.

D. Multi-Inheritance

Copy codeThe 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 to inherit js, such as using prototype chain. This does not belong to the scope of this article, but only describes the usage of call here. When we talk about call and apply, the two methods basically mean that the second parameter of call can be of any type, and the second parameter of apply must be an array, it can also be arguments.
Callee, caller ..

Summary:

To put it simply, the two methods have the same effect.

Differences: parameters passed by methods are different.

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.