Function: Apply Method

Source: Internet
Author: User

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: I am looking for an article about apply on the Internet. By the way, I will learn English. Haha, I will translate it. The first time I translate the article. If there is anything wrong, please advise me.

Original post address: http://blog.csdn.net/cinnxu/archive/2004/12/17/219503.aspx

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.