Grammar
Functionobject.apply ([Thisobj [, Argsarray]])
apply()The function is used to functionObject invoke the current function and can use thisObj the specified object as a this pointer reference inside the function when the function is executed at the same time.
This function belongs Function to the object and is supported by all major browsers.
Parameters
| Parameters |
Description |
| Thisobj |
The optional/object type specifies functionObject the object that the function internal pointer refers to when the function is executed this . |
| Argsarray |
An optional/array|argumens object functionObject that is passed into the parameter array or arguments object when the function is called. |
If a argsArray parameter is supplied, the argument must be an array, or an arguments object. Each element in the array (0...N per attribute in the arguments object) is passed in the function as a parameter in order.
If a argsArray parameter is supplied, the parameter must be supplied thisObj .
Note: This function is the same as the call () function of a function object, except that call() functions are passed in each of the parameters of a functional object, which apply() is a function that takes the parameters of a function object in an array or arguments The form of the object is passed in as a whole.
return value
apply()The return value of the method is any type, and its return value depends on functionObject The return value of the current object.
Example & Description
Name = "Zhang San"; Age= 18;functionTest () {Document.writeln ( This); Document.writeln ( This. Name); Document.writeln ( This. Age); };//This is the Global object window inside the global functionTest ();//[Object Window] 318varobj = {name: "John Doe", age:20};//change the internal this pointer reference object to objTest.apply (obj);//[Object Object] Lee 420functionFoo (A, b) {Document.writeln ( This. Name); Document.writeln (a); Document.writeln (b); }//change this reference to obj, passing two parameters at a timeFoo.apply (obj, [12,true]);//Lee 412 TruefunctionBar () {varo = {name: "Harry"}; //Call the Foo () function and change its this to the object o, passing in the current function's parameter object arguments as its argumentfoo.apply (o, arguments); }bar ("Codeplayer", "www.365mini.com");//Harry Codeplayer www.365mini.com
The JavaScript function.apply () function is detailed