【问题标题】:Less.js + IE8 = Object doesn't support property or method 'bind'Less.js + IE8 = 对象不支持属性或方法“绑定”
【发布时间】:2017-01-27 22:32:39
【问题描述】:

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

SCRIPT438:对象不支持属性或方法“绑定” 文件:less.js,行:1896,列:1

缩小版也是如此 SCRIPT438:对象不支持属性或方法“绑定” 文件:less.min.js,行:13,列:27226

我听说 IE8 及以下不支持绑定,因此问题。

谁能提供一个解决方案来解决这个问题,而不必完全转储 Less JS(不是一个选项)?

【问题讨论】:

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


【解决方案1】:

您可以为 bind 使用 polyfill,例如 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;
};

}

【讨论】:

  • 这是简单地将其添加到less.js文件的问题吗?我对 JS 非常基础,所以不确定这是简单的复制>粘贴修复还是需要集成。
  • 必须在使用bind的代码之前执行。在 less.js 之前加载的单独脚本将起作用。对于 IE8 不支持的功能,您可能需要其他 pollyfills。
  • 感谢文森特的帮助。它确实绕过了错误,但现在只显示另一个(SCRIPT438:对象不支持属性或方法'keys' - 文件:less.min.js,行:10,列:28054)。我想我会开始寻找替代解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多