METHOD:  Function::apply

Function.apply(thisArg[, argArray])

The apply method allows you to apply to a function a method from another function. This means you only need to write an object once and just apply it to any other objects that make use of it. The argArray parameter can be either an array literal or the deprecated arguments property of the function.

The following example first creates an object called Car which has three properties. Then a second object is created called HireCar which (beside others) also has those same properties. So, instead of having to rewrite those properties, the HireCar object uses the apply method to inherit them from the Car object. Note that you can assign a different This object when calling an existing function.

Code:
function car(make, model, year)
{this.make = make, this.model = model, this.year = year}

function hireCar(carNo, make, model, year)
{this.carNo = carNo, car.apply(this, make, model, year)}

NOTE:

The apply method is very similar to the call method and only differs in that, up until now, you could use the deprecated arguments array as one of its parameters.

Copyright 1999 by Infinite Software Solutions, Inc.
Trademark Information