The call () and apply () methods in JS

Source: Internet
Author: User

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. Difference between call () and apply () method

Each of the two methods is "borrowing constructors"

They differ only in the way in which parameters are accepted,other parameters of the call method are passed to the borrowed method as arguments, and apply, the second argument is passed as an array

3. Common examples

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); Note: The function in JS is actually an object, and the function name is a reference to a function object.

function Animal () {
THIS.name = "Animal";
This.showname = function () {
alert (this.name);
}
}

function Cat () {
THIS.name = "Cat";
}

var animal = new animal ();
var cat = new Cat ();

Using the call or Apply method, the ShowName () method that originally belonged to the animal object is given to the object cat.
The input result is "Cat"
Animal.showName.call (Cat, ",");
Animal.showName.apply (cat,[]);

Call means to put the animal method on the cat, the original cat is no ShowName () method, now is to put animal showname () method on the cat to execute, so this.name should be cat

Implementing inheritance

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 instead of the this object, so there is no Animal of all the properties and methods in Cat, and the Cat object can directly invoke the Animal method and properties.

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);
}

Multiple inheritance is achieved with two call

The call () and apply () methods in JS

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.