How to handle lack of JavaScript Object.bind() method in IE 8

前端 未结 4 2187
长情又很酷
长情又很酷 2020-11-27 07:13

I am writing a bit of JavaScript that uses the Object.bind method.

funcabc = function(x, y, z){ 
    this.myx = x;
    this.playUB = function(w)         


        
4条回答
  •  盖世英雄少女心
    2020-11-27 07:26

    Function.prototype.bind isn’t supported in Internet Explorer 8 and below. Compatibility chart here: http://kangax.github.io/es5-compat-table/

    Mozilla Developer Network provide this alternative for older browsers that don't implemented .bind() natively:

    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 be bound is not callable");
        }
    
        var aArgs = Array.prototype.slice.call(arguments, 1),
            fToBind = this,
            fNOP = function () {},
            fBound = function () {
              return fToBind.apply(this instanceof fNOP && oThis
                                     ? this
                                     : oThis,
                                   aArgs.concat(Array.prototype.slice.call(arguments)));
            };
    
        fNOP.prototype = this.prototype;
        fBound.prototype = new fNOP();
    
        return fBound;
      };
    }
    

提交回复
热议问题