JavaScript bind ()

Source: Internet
Author: User

A few days ago to see a face question, the title is this:
Would you please talk about the understanding of Apply,call,bind in JavaScript?
First of all, apply and call is a cliché, but for bind, I froze, because the word is a very high-frequency method used in jquery to bind the DOM elements to events.
In order to find out this unfamiliar and familiar bind,google, found in the javascript1.8.5 version of the native implementation of this method, currently ie9+,ff4+,chrome7+ support this method, opera and Safari is not supported (the description on MDN).
The role of BIND and apply,call is similar to changing the function's execute context, which is the point of the This keyword at runtime. However, the method of use is slightly different. A function can be executed later after bind.
Examples are as follows:

Copy CodeThe code is as follows:
var person = {
Name: ' Andrew ',
Job: ' Web Front end Developer ',
Gender: ' Male ',
Sayhello:function () {
Return ' Hi, I am ' + this.name + ', a ' + this.job;
}
}
Console.log (Person.sayhello ()); Hi, I am Andrew, a web front end developer
var Anotherguysayhello = Person.sayHello.bind ({
Name: ' Alex ',
Job: ' Back end C # Developer '
});
Console.log (Anotherguysayhello ()); Hi, I am Alex, a back end C # developer


Another example with parameters:

Copy CodeThe code is as follows:


function Add (arg1, arg2, Arg3, Arg4) {
return arg1 + "+ arg2 +" + arg3 + "+ arg4;
}
var Addmore = Add.bind ({}, ' A ', ' B ');
Console.log (Addmore (' C ', ' d ')); A B c D

JavaScript 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.