The JavaScript function.apply () function is detailed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.