不幸的是,我需要处理IE8兼容性的噩梦,经过几个小时的修复一个又一个的问题,我陷入了死胡同,希望有人可以帮助我。
Babel
通过此方法实现继承:
function _inherits(subClass, superClass) {
if ("function" != typeof superClass && null !== superClass) throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: !1,
writable: !0,
configurable: !0
}
}), superClass && (Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass);
}
当我在IE8上运行此代码时,出现此错误:Object doesn't support this property or method
指的是Object.create
的用法
我试图寻找一个插件和 Babel 的不同设置,但找不到真正解决它的东西。
有人知道如何处理它吗?
尽可能简单:Object.create 并非在所有浏览器中都可用(IE <10,较旧的 Opera 和 Chrome 版本(
我通过创建这个简单的 polyfill 来解决此问题:
if (!Object.create) {
Object.create = function(o, properties) {
if (typeof o !== 'object' && typeof o !== 'function') throw new TypeError('Object prototype may only be an Object: ' + o);
else if (o === null) throw new Error("This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument.");
//if (typeof properties != 'undefined') throw new Error("This browser's implementation of Object.create is a shim and doesn't support a second argument.");
function F() {}
F.prototype = o;
return new F();
};
}
参考: Object.create 在 ie8 中不受支持
我评论了if (typeof properties != 'undefined')
行,因为如果没有,我会收到以下错误:
此浏览器的 Object.create 实现是一个填充程序,没有 支持第二个论点。
我认为它不是那么安全,因为如果某些消费者将其与第二个参数(properties
(一起使用,可能会导致意外行为,但对于我的用例来说没关系。
目前为止,一切都好。