Call (), apply (), bind () method,

Source: Internet
Author: User

First, call () and the Apply () method:

In JavaScript advanced programming, the function is also an object, and the top layer of the function prototype chain is actually pointing to object. Since the function is an object, then nature has properties and methods. "Each function contains two non-inherited methods called call (), apply ()"

Call (), apply (), the essence of these two methods is to set the function body this object points to the value, in other words to understand this sentence, an object can be called () and apply () to invoke other object methods. For example, object A has a Sayname method, but object B does not have this method, and normally we cannot call the Sayname method in object B, if we do not want to define a Sayname method for object B, it is possible to use calls (), apply ()

object does not need to have any coupling relationship with the method!

functionPerson () {}; Person.prototype={name:' Jersey ', Sayname:function() {Console.log ( This. Name); }    } varPerson1 =NewPerson ();p erson.sayname (); //JerseyvarCar ={name:' Car ';     }car.sayname (); //wrong!Person1.sayName.call (car);//CarPerson1.sayName.apply (car);//Car//this point in the function Sayname points to carWindow.color= ' Red ';varo ={color: ' Biue '}functionsay () {Console.log ( This. color);//Red Global Environment in this point to window}say.call ( This);//Red Sets the This in the say function.Say.call (window);//Red Sets the this point in the say function to the windowSay.call (o);//Biue Set the this point in the Say function to Object o, which is the execution environment of the Say function changes

The call () and the Apply () methods have the same effect, and they differ only in the way they receive parameters. (Not much to say here)

Bind () Method:

Using the Bind () method creates an instance of a function (because the function is an object), and the This value of the function instance is bound to the first parameter of the incoming bind () method

Window.color = ' red '; var o = {color: ' Blue '}; function say () {    Console.log (this. color);    // Red }var saycolor = say.bind (o);    First, an instance of a function is created, the this value of the original say function is bound to the object o, and the function instance is saved to the variable Saycolor    saycolor (); // Blue     

Using the call () and the Apply () method immediately executes a function that calls both methods, and using the bind () method, a function instance is created that executes when the function instance is called.

In fact, to understand the three methods the most important thing is to understand the function object, think of the function as a normal object, and then call () apply () bind () as the method of this object to change something inside this function

Call (), apply (), bind () method,

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.