A summary of the use of Bind method in ECMAScript5 (ES5)

Source: Internet
Author: User
Tags bind

This article mainly describes the use of the Bind method in ECMAScript5 (ES5), like bind and call, and apply, all of which can change the context of this point, need friends can refer to the following

It has always been vague about this, such as call, apply, and so on. This time I saw a pen question related to bind, so I remember this article in case of forgetting.

Bind and call, as well as apply, can change this point of the context. The difference is that call and apply are referred directly to the method, and bind binds this to return a method, but the internal core is still apply.

See examples directly:

The code is as follows:

var obj = {

A:1,

B:2,

Getcount:function (c, D) {

Return THIS.A + this.b + C + D;

}

};

WINDOW.A = window.b = 0;

Console.log (Obj.getcount (3, 4)); 10

var func = Obj.getcount;

Console.log (Func (3, 4)); 7

Why is this so? Because the Func in the context of this is window!bind exists precisely to change this point to get the desired value:

The code is as follows:

var obj = {

A:1,

B:2,

Getcount:function (c, D) {

Return THIS.A + this.b + C + D;

}

};

WINDOW.A = window.b = 0;

var func = obj.getCount.bind (obj);

Console.log (Func (3, 4)); 10

Bind is a function extension method for functions that bind the code back to this point (obj) inside the func, but incompatible ie6~8, which is compatible with the following code:

The code is as follows:

var obj = {

A:1,

B:2,

Getcount:function (c, D) {

Return THIS.A + this.b + C + D;

}

};

Function.prototype.bind = Function.prototype.bind | | function (context) {

var that = this;

return function () {

Console.log (arguments); console [3,4] if ie<6-8>

Return that.apply (context, arguments);

}

}

WINDOW.A = window.b = 0;

var func = obj.getCount.bind (obj);

Console.log (Func (3, 4)); 10

Actually, it seems to me that the core of BIND is to return an unassigned method if you use apply or call directly:

The code is as follows:

var ans = obj.getCount.apply (obj, [3, 4]);

Console.log (ANS); 10

Cannot be constructed with the shorthand func function, so it is quite ingenious to pass the this point with bind and return an execution method.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.