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

如何处理IE 8中缺少JavaScript Object.bind()方法的问题,javascript,internet-explorer-8,cross-browser,bind,backwards-compatibility,Javascript,Internet Explorer 8,Cross Browser,Bind,Backwards Compatibility,我正在编写一些使用Object.bind方法的JavaScript 由于我使用Firefox在WinXP中开发,有时使用IE9或IE10在Win7中测试,所以我没有注意到IE8及以下版本不支持绑定 这个特定的脚本不使用画布,所以我有点犹豫是否要注销所有IE8用户 有标准的工作环境吗 我在JavaScript中的表现还算不错,但我还是有点不在行。因此,如果解决方案是显而易见的,请原谅我 此页面上有一个很好的兼容性脚本: 只需复制并粘贴到脚本中即可 编辑:为了清晰起见,将脚本放在下面 if (!F

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

由于我使用Firefox在WinXP中开发,有时使用IE9或IE10在Win7中测试,所以我没有注意到IE8及以下版本不支持绑定

这个特定的脚本不使用画布,所以我有点犹豫是否要注销所有IE8用户

有标准的工作环境吗


我在JavaScript中的表现还算不错,但我还是有点不在行。因此,如果解决方案是显而易见的,请原谅我

此页面上有一个很好的兼容性脚本:

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

编辑:为了清晰起见,将脚本放在下面

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

最好的解决方案可能是安装

Modernizer告诉您当前浏览器是否本机实现了此功能 它还提供了一个脚本加载器,因此您可以拉入polyfill来回填旧浏览器中的功能

以下是生成Modernizer自定义版本的链接:

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

var foo=functionx,y,z{return functionx,y,z,return Math.max.callthis,x,y,zx,y,z} var bar=functionx,y,z{返回functionx,y,z,return Math.min.callthis,x,y,zx,y,z} 控制台1,2,3;
控制台.日志条3,2,1 Internet Explorer 8及以下版本不支持Function.prototype.bind。兼容性图表如下:

Mozilla Developer Network为未实现的旧浏览器提供了此替代方案。本机绑定:

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

@micha,是的,缺少:在以下文档模式中不受支持:怪癖、Internet Explorer 6标准、Internet Explorer 7标准、Internet Explorer 8标准。这非常有效。找到了绑定问题的解决方案,并学会了在mozilla文档中查找关键字兼容性。顺便说一句,IE 8缺少了太多的功能。基本上,我需要坚持使用兼容HTML5的浏览器。如果没有便捷的音频,就没有意义了。@alex-你知道IE10是否支持绑定,或者它是否需要你提到的工作吗?@pure_code-我想是的,绑定方法已经成为标准,所以所有新浏览器都应该有它。但我还没有测试过它,现在也无法访问IE10。在上面贴了一个问题…用浏览器兼容性图表回答得很好。如何检查当前浏览器是否本机实现了此功能或未使用Modernizer?此答案是完全错误的,而且有些正确!这是完全错误的,因为Modernizer不提供函数绑定可用性的测试,可能是因为测试非常简单,只需检查function.prototype.bind!==未定义。然而,这有点正确,因为Modernizer实际上包含一个函数bind polyfill本身!此处包含的详细信息:Modernizer>3现在不再包含用于绑定的polyfill
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;
  };
}