Bind () Method:
A. ECMAScript5 also defines a method: Bind (). This method creates an instance of a function whose this value is bound to the value passed to the bind () function.
B. Browsers that support the bind () method are ie9+, firefox4+, safari5.1+, opera12+, and Chrome.
Window.color= "Red"; varO={color: "Blue"}; //global function DefinitionfunctionSaycolor () {Console.log ( This. color);} //The bind () method creates an instance of a function whose this value is bound to the value passed to the bind () function.varObjectsaycolor=Saycolor.bind (o); Objectsaycolor ();//BluevarMYOBJ ={specialfunction:function() {}, Anotherspecialfunction:function() {}, Getasyncdata:function(CB) {CB (); }, Render:function () { varthat = This; This. Getasyncdata (function() {that.specialfunction (); That.anotherspecialfunction (); }); }}; Myobj.render ();//-----------------------------------------------------varMYOBJ ={specialfunction:function() {}, Anotherspecialfunction:function() {}, Getasyncdata:function(CB) {CB (); }, Render:function () { This. Getasyncdata (function () { This. Specialfunction (); This. Anotherspecialfunction (); }.bind ( This)); }}; Myobj.render ();//-----------------------------------------------------Function.prototype.bind () internal working principle: Function.prototype.bind=function(scope) {varfn = This; return function () { returnfn.apply (scope); };}//-----------------------------------------------------varFoo ={x:3} varBar =function() {Console.log ( This. x);} Bar (); //undefined varBoundfunc =Bar.bind (foo); Boundfunc ();//3
Reference: http://blog.jobbole.com/58032/
JavaScript bind ()