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

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

本文关键字:JavaScript bind 方法 Object 何处理 处理 IE      更新时间:2023-09-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);
};

由于我使用 Firefox 在 WinXP 中开发,有时使用 IE 7 或 10 在 Win10 中进行测试,我没有注意到或注意 IE8 及更低版本不支持 bind 的事实。

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

是否有标准的解决方法?

我在 JavaScript 中走来走去还不错,但我仍然有点菜鸟。因此,如果解决方案完全明显,请原谅我。

此页面上有一个很好的兼容性脚本:https://developer.mozilla.org/en/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;
  };
}

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

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

以下是生成现代化自定义版本的链接:
http://modernizr.com/download/#-teststyles-testprop-testallprops-hasevent-prefixes-domprefixes

Function.prototype.bind 在 Internet Explorer 8 及更低版本中不受支持。此处的兼容性图表: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;
  };
}

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

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

引用

    函数
  • 式 JavaScript:利用函数对象的强大功能
  • eval() 不是邪恶的,只是被误解了
  • 通过函数构造函数创建的函数的 [范围]
  • John Resig - Fast JavaScript max/min