JavaScript Object.create in old IE

后端 未结 1 461
抹茶落季
抹茶落季 2021-01-29 02:06

Is there a way to make this code backwards compatible with IE6/7/8?

function Foo() {
    ...
}

function Bar() {
    ...
}

Bar.prototype = Object.create(Foo.pro         


        
相关标签:
1条回答
  • 2021-01-29 02:37

    Pointy's comment as an answer

    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();
        };
    }
    

    This polyfill covers the main use case which is creating a new object for which the prototype has been chosen but doesn't take the second argument into account.

    0 讨论(0)
提交回复
热议问题