首页>Program>source

我正在写一些使用 Object.bind的JavaScript 方法。

funcabc = function(x, y, z){ 
    this.myx = x;
    this.playUB = function(w) {
        if ( this.myx === null ) {
            // do blah blah
            return;
        }
        // do other stuff
    };
    this.play = this.playUB.bind(this);
};

由于我是使用Firefox在winXP中进行开发的,有时是在IE 9或10的win7中进行测试的,所以我没有注意到或注意IE8及以下版本不支持 bind

此特定脚本不使用画布,因此我有点犹豫要注销所有IE 8用户.

有标准的解决方法吗?

我在JavaScript方面还可以,但是还是有点菜鸟.所以,如果解决办法很明显,请原谅我.

最新回答
  • 2021-1-11
    1 #

    此页面上有一个不错的兼容性脚本: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

    只需将其复制并粘贴到脚本中即可。

    EDIT: 为了清楚起见,将脚本放在下面。

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

  • 2021-1-11
    2 #

    最好的解决方案是安装Modernizr.

    Modernizr告诉您当前浏览器是否具有本机实现的此功能 并且它提供了脚本加载器,因此您可以拉入polyfill来在旧版浏览器中回填功能.

    以下是生成您的modernizr自定义版本的链接:
    Internet Explorer 8及更低版本不支持http://modernizr.com/download/#-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes

  • 2021-1-11
    3 #

    Function.prototype.bind.此处的兼容性图表:http://kangax.github.io/es5-compat-table/

    Mozilla开发人员网络为未原生实现.bind()的旧版浏览器提供了这种替代方法:

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

  • 2021-1-11
    4 #

    Function构造函数是执行此操作的老式方法:

    var foo = function(x,y,z){ return Function("x,y,z","return Math.max.call(this, x, y, z)")(x,y,z) }
     
    var bar = function(x,y,z){ return Function("x,y,z","return Math.min.call(this, x, y, z)")(x,y,z) }
     
    console.log(foo(1,2,3) );
     
    console.log(bar(3,2,1) );
    

    References

      Functional JavaScript: Harnessing the power of the Function Object

      eval() isn’t evil, just misunderstood

      [Scope] of Functions Created via the Function constructor

      John Resig - Fast JavaScript Max/Min

相关问题

  • bash:从管道读取的while读取循环后,变量将重置
  • JavaScript中的单加号运算符