in JavaScript, we often use function bindings, and when you need to keep this context in another function, it's convenient to use Function.prototype.bind ()
In the past, you might have directly set up self=this or That=this and so on, which would certainly work, but using Function.prototype.bind () would be better and look more professional. Here's a simple example: The code is as follows: var myobj = { specialfunction:function () { }, anothers Pecialfunction:function () { }, Getasyncdata:function (CB) { CB ( ); }, render:function () { var = this; &NB Sp This.getasyncdata (function () { that.specialfunction (); &NB Sp that.anotherspecialfunction (); }); }}; Myobj.render (); In this example, in order to maintain the myobj context, set a variable that=this, which is feasible, but does not use Function.prototype.bind () looked neater: The code is as follows: Render: function () { this.getasyncdata (function () { this.specialfunction (); &NB Sp this.anotherspecialfunction (); }.bind (This)); } when calling. Bind (), it simply creates a new function and then passes this to this function. The code for Implementing. Bind () is probably like this: the code is as follows: Function.prototype.bind = Function (scope) { var fn = this; &NB Sp return function () { return fn.apply (scope); }; Below look at a simple example of using Function.prototype.bind (): Code is as follows: var foo = { x:3}; var bar = function () { console.log (this.x); Bar (); Undefined var boundfunc = Bar.bind (foo); Boundfunc (); 3 is not very easy to use it! Unfortunately, IE8 and the following IE browsers do not support Function.prototype.bind (). The supported browsers have Chrome 7+,firefox 4.0+,ie 9+,opera 11.60+,safari 5.1.4+. Although not supported by browsers such as IE 8/7/6, the Mozilla Development Group wrote a function similar to the old version of IE, with the following code: Code as follows: if (! Function.prototype.bind { Function.prototype.bind = function (othis) { if (typeof this!==) function ") { //closest thing possible to the ECMAScript 5 internal iscallable function   throw new TypeError ("Function.prototype.bind-what is trying to being bound not callable"); } var Aargs = Array.prototype.slice.call (arguments, 1), &NBS P Ftobind = this, FNOP = function () {}, , Fbound = function () { return ftobind.apply (this instanceof fnop && othis &NBSP ; &NBSP This &NBSP ;: Othis, a Args.concat (Array.prototype.slice.call (arguments))); }; Fnop.prototype = This.prototype; Fbound.prototype = new Fnop (); &NBSP return fbound; }; }