Less.js + IE8 =对象不支持属性或方法'bind'

时间:2016-09-20 05:54:43

标签: javascript internet-explorer-8 less bind ie8-browser-mode

在进行一些跨浏览器测试(在IE8模式下使用IE Edge)时,由于Less JS(v2.7.1)出错,页面无法正确呈现。控制台日志是:

SCRIPT438:对象不支持属性或方法'bind' 文件:less.js,行:1896,专栏:1

缩小版也一样 SCRIPT438:对象不支持属性或方法'bind' 文件:less.min.js,行:13,专栏:27226

我已经读过IE8及以下版本不支持绑定因此问题。

任何人都可以提供解决方案,解决我如何解决这个问题,而不必完全转储Less JS(不是一个选项)?

1 个答案:

答案 0 :(得分:0)

您可以使用bind的填充,例如MDN's one。如链接中所述,与原生的存在一些差异。

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
                   ? this
                   : oThis,
                   aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    if (this.prototype) {
        // Function.prototype doesn't have a prototype property
        fNOP.prototype = this.prototype; 
    }
    fBound.prototype = new fNOP();

    return fBound;
};

}