JavaScript learning note: function :: applly method

xiaoxiao2021-03-06  45

Method: Function :: Apply

-------------------------------------------------- ----------------------------- Function.Apply (thisobj [, argarray])

The Apply method allows a method of calling an object and replaces the current object with a specified object.

Parameter Thisobj options. Will be used as the object of the current object. ARGARRAY options. The parameter array 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 should 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 RentalCar is defined. RentalCar will inherit the properties of Car and add one additional property of its own -. carNo The RentalCar 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 RentalCar object being constructed, and not. A New Car Object. by this Means, The Rentalcar Object Inherits The Properties from the car object.

The following example simulates the inheritance of an object with the Apply method. First define a construction method of a Car object, there are three properties. The second definition is defined for the construction method of a Rentalcar object, and Rentalcar will inherit the attribute of Carller and plus one yourself. The attribute carno. Rentalcar constructor uses the Apply method to call the CAR constructor to pass itself as a THISARG parameter. Therefore, in the inside of the Car function, the keyword THIS is actually replaced by the Rentalcar object, not a new car Object. With this method, the Rentalcar object inherits its properties from the Car object.

Function Car (Make, Model, Year) {this.make = make; this.model = model; this.year = year;} Function Rentalcar (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 hardware in That, Up Until Now You Could Use the Deprecated Arguments Array As One of Its Parameters.note: Apply Method Method Like Call, the unique difference is the parameter passed by the Apply method is arguments or array object PS: an article on the Internet About Apply Articles By the way, learn English, huh, I translated the article, the first translation article, if there is anything wrong, please advise

转载请注明原文地址:https://www.9cbs.com/read-79719.html

New Post(0)