Introduction to the Prototype.bind () method in JavaScript

Source: Internet
Author: User

  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     &nbsp 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;  }; }

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.