Function.apply() (Method)

Uses a function object as if it belonged to another target object.

Availability:

ECMAScript edition - 3
JavaScript - 1.3
JScript - 5.5
Internet Explorer - 5.5
Netscape - 4.06
Property/method value type:Function result
JavaScript syntax:-myFunction.apply(anObject, anArray)
Argument list:anArrayA set of arguments presented as an array
anObjectAn object to apply the function to

If you want to force a particular function implementation to be used on an object, you can locate the one you want and pass it an object on which to operate. The function then executes as if it were a property of the passed object instead of the receiving object.

This can be useful to restore some functionality that may have been overridden or to share a function definition between several objects.

This is effectively like adding the function as a property to the target object, executing it, passing the arguments to it and then deleting the function property afterwards. Applying a function is a lot easier.

This is similar to the call() method that passed the arguments as a comma separated list. This passes them as an array.

Example code:

   // Use the fundamental valueOf method on an object that has overridden
   // its own valueOf method
   Object.prototype.valueOf.apply(myTargetObject);

See also:Element.applyElement()

Cross-references:

ECMA 262 edition 3 - section - 15.3.4.3