A brief description of call (), apply (), bind ()

Source: Internet
Author: User

Call (Thisobj,arg1,arg2 ...), apply (Thisobj,[obj1,obj2 ...]) These two methods are non-inherited methods that each function contains
Call (thisobj[, args]) and apply (thisobj[, args])
The action is the same, simply to change the current object in the method using this point, point to the Thisobj object in the calling method the difference between the two (the first parameter is the same) is the call method passed in the parameters are listed, And the second parameter in the Apply method is an array
Or an example of the more intuitive:

Window.color= ' Red ';
var o={color: "Blue"};
function Saycolor () {
alert (This.color);
};
Saycolor (); Red (global function, this is window)
Saycolor.call (this);//red (Invoke Call method, specify object is this, here is window, no meaning)
Saycolor.call (window);//red (call method is called, specifies that the object is window, it doesn't make sense)
Saycolor.call (o); Blue (call method is called, the specified object is O, so this refers to the object o, where the original window points to O)
Saycolor.apply (o);//blue (call method is called, the specified object is O, so this refers to the object o, where the original window points to O)
The bind () method in ECMAScript5 is similar to the first two methods, and the bind () method creates an instance of a function that is bound to the value passed to the bind () function.

Example:
function A (y) {
return this.x+y;
};
var o={x:1};
var g=a.bind (o);
g (2);//3
From the example, we can see that function A is bound to the object o, and the new function g is returned, and when G is called, a function is called as the object O method.
Bind () This method binds a function to an object and returns a new function in which the parameters passed into the new function are passed to the bound function.

A brief description of call (), apply (), bind ()

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.