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.
function Add (A, b) { alert (a+b); } function sub (b) { alert (a-b); } Add.call (Sub,3,1); 4
function Animal () { This. Name ="Animal"; This. ShowName =function () {alert ( This. Name); }} function Cat () { This. Name ="Cat"; } varAnimal =NewAnimal (); varCat =NewCat (); //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,[]);
The call () and apply () methods in JS