这个 bind 方法只有在 ie10 版本的浏览器才得到原生支持,低于该版本的浏览器下执行时会得到一个 undefined 的错误提示。

于是只好再次上网 google 解决方案,功夫不负有心人,我们在 firefox 的开发站找到了解决方案,那就是增加 property 原型使得所有浏览器都能支持 bind 方法,代码如下:

if (!Function.prototype.bind) {
        Function.prototype.bind = function (oThis) {
            if (typeof this !== "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-09-01
  • 2021-09-14
  • 2021-05-13
  • 2021-09-30
  • 2021-07-10
  • 2021-08-07
  • 2021-08-17
  • 2021-07-13
猜你喜欢
  • 2021-12-08
  • 2022-12-23
  • 2021-11-28
  • 2021-11-29
  • 2021-04-14
  • 2021-11-29
  • 2021-04-01
相关资源
相似解决方案