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