如何处理IE 8缺乏JavaScript Object.bind()方法的问题

26

我正在编写一些使用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);
};

由于我使用WinXP开发Firefox,有时在Win7上测试IE 9或10,因此并没有注意到或关注到IE8及以下版本不支持bind

这个脚本没有使用canvas,所以我有点犹豫是否放弃所有的IE 8用户。

是否有标准的解决方法?

我在JavaScript方面还算可以,但我还是一个新手。如果解决方案非常明显,请原谅我。


@micha,是的,缺失:不支持以下文档模式:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准、Internet Explorer 8 标准。 - Alexander
4个回答

51

这个页面有一个很好的兼容性脚本: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/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;
  };
}

2
那个运行得非常好。找到了解决绑定问题的方法,并学会在Mozilla文档中寻找关键词“兼容性”。 - Claude
顺便说一下,IE 8缺少太多功能了。基本上,我需要坚持使用HTML5兼容的浏览器。没有方便的Audio(),就没有意义了。 - Claude
@alex - 你知道IE10是否支持bind,还是需要你提到的解决方法? - user1637281
@pure_code - 我会认为它可以(bind方法已经成为标准,所以所有新的浏览器都应该有它)。但是我没有测试过,并且现在也无法访问IE10。 - alexwells
我在Stack Overflow上发布了一个问题,询问哪些浏览器支持bind()方法。得到了一个很好的答案,并附有浏览器兼容性表格。 - user1637281
显示剩余3条评论

4

如何使用Modernizer检查当前浏览器是否本地实现了此功能? - dragonfly
3
这个答案既有对又有错!它的错误在于 Modernizr 并没有提供一个测试函数绑定可用性的方法(可能是因为这个测试很简单,只需检查 Function.prototype.bind !== undefined 即可)。然而,它的部分正确之处在于 Modernizr 实际上包含了一个函数绑定 polyfill!关于它的包含细节可以看这里:https://github.com/Modernizr/Modernizr/issues/478 - WickyNilliams
现代浏览器检测库 Modernizr > 3 不再包含 bind 的 polyfill。 - gunnx

2

Function.prototype.bind在Internet Explorer 8及以下版本中不受支持。 兼容性表在此处:http://kangax.github.io/es5-compat-table/

Mozilla Developer Network为那些没有原生实现.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;
  };
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接