0

有没有办法让这段代码向后兼容 IE6/7/8?

function Foo() {
    ...
}

function Bar() {
    ...
}

Bar.prototype = Object.create(Foo.prototype);

主要问题是Object.create错误,然后浏览器崩溃。

那么是否有一个函数可以用来模拟Object.create旧 IE 的行为。或者我应该如何重新编码上面的代码来解决它?

4

1 回答 1

1

Pointy的评论作为答案

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create#Polyfill

if (!Object.create) {
    Object.create = function (o) {
        if (arguments.length > 1) {
            throw new Error('Object.create implementation only accepts the first parameter.');
        }
        function F() {}
        F.prototype = o;
        return new F();
    };
}

这个 polyfill 涵盖了创建一个新对象的主要用例,该对象的原型已被选择,但不考虑第二个参数。

于 2013-02-13T23:21:24.410 回答