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 ()