apply()The function is used to invoke the current function functionObject and can use the specified object thisObj as a pointer reference inside the function when the function is executed at the same time this .
This function belongs Function to the object and is supported by all major browsers.
Grammar
Functionobject. Apply([[,]])
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 argumentsthe 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] 318
VarObj= {Name: "John Doe",Age: 20};
Change the internal this pointer reference object to obj
Test.Apply(Obj);
[Object Object] Lee 420
functionFoo(A,B){
Document.Writeln(This.Name);
Document.Writeln(A);
Document.Writeln(B);
}
Change this reference to obj, passing two parameters at a time
Foo.Apply(Obj, [12, True]);
Lee 412 True
functionBar(){
var o = {name: "Harry" };< Span class= "PLN" >
//call the Foo () function and change its this to object o, passing in the current function's parameter object arguments as its argument
Foo. Applyo, Arguments
}
Bar ( " Codeplayer ", " www.365mini.com "
//Harry Codeplayer www.365mini.com
JavaScript function.apply () function detailed