Understanding of the call () method in JavaScript

Source: Internet
Author: User

Call (Thisobj [, Arg1 [, arg2 [, [, ArgN]]])

Call () Method: The official introduction is to invoke one method of an object, replacing the current object with another object.

The call () method is applied to a function object and can be used in place of another object to invoke a method that changes the object context of a function from the initial context to the new object specified by Thisobj. If the Thisobj parameter is not provided, then the global object is used as the thisobj.

It is difficult to understand the words directly. Let's look at an example:

function Class1 () {        this. Name = ' fine ';           This function () {            Console.log (this. Name);            Console.log (this);        };        Console.log (this);     }
Instantiate constructor varnew Class1 (); Class1.showname ();

The result of testing in Chrome is:

You can see that the values of the two console.log (this) are the same, pointing to the constructor;

And then we'll modify the example above:

functionClass1 () { This. Name = ' fine ';  This. ShowName =function() {Console.log ( This. Name); Console.log ( This);        }; Console.log ( This); }    functionClass2 () { This. Name = ' OK '; }    //create the appropriate instance    varClass1 =NewClass1 (); varClass2 =NewClass2 (); Class1.showName.call (class2);//the call method must be applied to the function object

Test the following in the browser:

The first Class1 object is Console.log (this) that is located outside the ShowName method in the Class1 constructor, after the call method is used,

  Class1.showName.call (Class2);

This, which is located outside the ShowName method, still points to Class1.

The next OK and Class2 objects are located inside the ShowName method, and this point has been changed to point to the Class2 constructor. As can be seen, the call method changes the direction of the this pointer in the function it applies to.

For the time being the call method understanding is this, later has the other understanding to fill up, may also have the wrong place welcome to add ~ ~

Reference to Bparadise's blog post: http://www.cnblogs.com/wuyuetian/p/4999723.html

Understanding of the call () method in JavaScript

Related Article

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.