Js call () and apply () Methods

Source: Internet
Author: User
Because it is rarely used, I have never had a deep understanding of call () and apply () in js. I recently reviewed [JavaScriptTheDefinitiveGuide6thEdition] to understand what the two methods are doing. There are two paragraphs in the book that are clearly written, which can be directly referenced and translated. Please forgive me for poor translation... SyntaxHighlighte

Because the call () and apply () of js are rarely used, I recently reviewed [JavaScript The Definitive Guide 6th Edition] to understand what The two methods are doing. There are two paragraphs in the book that are clearly written, which can be directly referenced and translated. Please forgive me for the poor translation:

1. both methods allow you to explicitly specify the this value for the invocation, which means you can invoke any function as a method of any object, even if it is not actually a method of that object.

Both methods allow you to explicitly specify the this attribute of the called function. this means that you can call any function as any "object function, even if this object does not actually define this function.

Continue to the second paragraph:

2. the first argument to both call () and apply () is the object on which the function is to be invoked; this argument is the invocation context and becomes the value of the this keyword within the body of the function.

The first parameters of call () and apply () are used to specify the object on which the function is executed. This object is actually called. In fact, both methods are doing one thing: change the value of this variable of the function.

Examples are easier to understand:

Example 1:


[Javascript]
Var name = 'out ';
Function show ()
{
This. name = 'in ';
This. print = function () {console. log (this. name );}
}
Var s = new show ();
S. print (); // 1
S. print. call (window); // 2

Var name = 'out ';
Function show ()
{
This. name = 'in ';
This. print = function () {console. log (this. name );}
}
Var s = new show ();
S. print (); // 1
S. print. call (window); // 2 output in sequence: in, out

Note 1 calls print to print the name attribute of the s object. Note 2 uses the call method to change the object referred to by this. Now it is a window, so it prints a window. name, that is, "out"

Example 2:

Write a simple function as follows:


[Javascript]
Function show ()
{
Console. log (arguments. slice (1 ));
}

Function show ()
{
Console. log (arguments. slice (1 ));
} You can put it in the chrome console for execution, and then call show (), you will be prompted: TypeError: Object #Has no method 'slice 'because the arguments object does not have the slice method.

However, if it is written as follows:


[Javascript]
Function show ()
{
Console. log (Array. prototype. slice. call (arguments, 1 ));
}

Function show ()
{
Console. log (Array. prototype. slice. call (arguments, 1 ));
} Then execute show (1, 2), and the [2] will be printed. This is equivalent to giving arguments a magical way to use the slice method.

Of course, call and apply are a little different. They are mainly used to transmit parameters. The comparison is as follows:

Call (obj, arg1, arg2 ,...)

Apply (obj, [arg1, arg2,...]) // transmits an array.

 


 

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.