Method: function: Apply
--------------------------------------------------------------------------------
Function. Apply (thisobj [, argarray])
The apply method allows you to call a method of an object and replace the current object with the specified object.
Parameters
Thisobj
Optional. Will be used as the object of the current object.
Argarray
Optional. Array of parameters that will be passed to the function.
The apply method allows you to call a function and specify what the keyword this will refer to within the context of that function. the thisarg argument shoshould be an object. within the context of the function being called, this will refer to thisarg. the second argument to the apply method is an array. the elements of this array will be passed as the arguments to the function being called. the argarray parameter can be either an array literal or the deprecated arguments property of a function.
The apply method can be used to simulate Object Inheritance as in the following example. we first define the constructor for an object called car which has three properties. then the constructor for a second object called into alcar is defined. rentalcar will inherit the properties of car and add one additional property of its own-carno. the specified alcar constructor uses the apply method to call the car constructor, passing itself as thisarg. therefore, inside the car function, the keyword this actually refers to the specified alcar object being constructed, and not a new car object. by this means, the specified alcar object inherits the properties from the car object.
The following example uses the apply method to simulate the inheritance of an object. first, define the construction method of a car object. There are three attributes. second, define a constructor for the rentalcar object. The rentalcar inherits the property of the car and adds its own property carno. the rentalcar constructor uses the apply method to call the car constructor and transmits itself as the thisarg parameter. therefore, inside the car function, the keyword "this" has been replaced by an internalcar object instead of a new car object. in this method, the rentalcar object inherits its attributes from the car object.
Code:
Function car (make, model, year)
{
This. Make = make;
This. Model = model;
This. Year = year;
}
Function compute alcar (carno, make, model, year)
{
This. carno = carno;
Car. Apply (this, new array (make, model, year ));
}
Mycar = new rentalcar (2134, "Ford", "Mustang", 1998 );
Document. Write ("your car is a" + mycar. Year + "" + mycar. Make + "" + mycar. Model + ".");
Output:
Your car is a 1998 Ford Mustang.
Note: The apply method is very similar to the call method and only differs in that, up until now, you cocould use the deprecated arguments array as one of its parameters.
Note: The apply method is very similar to the call method. The only difference is that the parameters passed by the apply method are arguments or array objects.
PS: An Introduction to applyArticleBy the way, I want to learn English. I will translate the article for the first time. If there is anything wrong with it, please advise me more.