Home >Web Front-end >JS Tutorial >Summary of the use of bind method in ECMAScript5 (ES5)_Basic knowledge
I have always been vague about things related to this, such as call, apply, etc. This time I saw a written test question related to bind, so I wrote this article as a reminder.
Bind, like call and apply, can change the this pointed to by the context. The difference is that call, like apply, refers directly to the method, while bind returns a method after binding this, but the internal core is still apply.
Look directly at the example:
Why is this happening? Because this in the context of func is window! The existence of bind is to change this pointer to obtain the desired value:
Bind is a function extension method of function. After bind, the code rebinds the this pointer (obj) inside func, but it is not compatible with ie6~8. The compatible code is as follows:
In fact, in my opinion, the core of bind is to return an unexecuted method. If you use apply or call directly: