Differences between Javascriptcall and apply and their usage _ basic knowledge

Source: Internet
Author: User
In JavaScript, call or apply is used to call a method instead of another object. The object context of a function is changed from the initial context to the new object specified by thisObj. I. method definition
Call method:
Syntax: fun. call (thisArg [, arg1 [, arg2 [,...])
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 thisArg.
If the thisArg parameter is not provided, the Global object is used as thisArg.

Apply method:
Syntax: fun. apply (thisArg [, argsArray])
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 thisArg is provided, the Global object will be used as thisArg and cannot be passed with any parameters.

Ii. Differences between the two
The basic difference between the two methods is that passing parameters is different.
2.1 call methods:

The Code is as follows:


Function Product (name, price ){
This. name = name;
This. price = price;
If (price <0)
Throw RangeError ('could not create product "'+ name +'" with a negative price ');
Return this;
}

Function Food (name, price ){
Product. call (this, name, price );
This. category = 'food ';
}
Food. prototype = new Product ();

Function Toy (name, price ){
Product. call (this, name, price );
This. category = 'toy ';
}
Toy. prototype = new Product ();

Var cheese = new Food ('feta ', 5 );
Var fun = new Toy ('Robot ', 40 );


2.2. the apply method:

The Code is as follows:


Function Product (name, price ){
This. name = name;
This. price = price;
If (price <0)
Throw RangeError ('could not create product "'+ name +'" with a negative price ');
Return this;
}

Function Food (name, price ){
Product. apply (this, arguments );
This. category = 'food ';
}
Food. prototype = new Product ();

Function Toy (name, price ){
Product. apply (this, arguments );
This. category = 'toy ';
}
Toy. prototype = new Product ();

Var cheese = new Food ('feta ', 5 );
Var fun = new Toy ('Robot ', 40 );


Iii. Role instances

3.1. class inheritance

The Code is as follows:


Function Person (name, age ){
This. name = name;
This. age = age;
This. alertName = function (){
Alert (this. name );
}
This. alertAge = function (){
Alert (this. age );
}
}

Function webDever (name, age, sex ){
Person. call (this, name, age );
This. sex = sex;
This. alertSex = function (){
Alert (this. sex );
}
}

Var test = new webDever ("design honeycomb", 24, "male ");
Test. alertName (); // design the honeycomb
Test. alertAge (); // 24
Test. alertSex (); // male


3.2. Callback Function

The Code is as follows:


Function Album (id, title, owner_id ){
This. id = id;
This. name = title;
This. owner_id = owner_id;
};
Album. prototype. get_owner = function (callback ){
Var self = this;
$. Get ('/owners/' + this. owner_id, function (data ){
Callback & callback. call (self, data. name );
});
};
Var album = new Album (1, 'Design honeycomb ', 2 );
Album. get_owner (function (owner ){
Alert (The album '+ this. name + 'belongs to' + owner );
});

Related Article

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.