How was Object.create() with second parameter accomplished prior to ECMAScript 5?

后端 未结 1 2030
既然无缘
既然无缘 2021-01-23 21:57

When dealing with Object.create() I came across this question and in summary it mentions:

  • polyfill that was used to retrofit IE8 with Object.cre
1条回答
  •  一向
    一向 (楼主)
    2021-01-23 22:30

    how did programmers accomplish this task prior to ES5

    Not at all. There had been property attributes in ES3, but there was no way to set them. There was no Object.create as well. There just had been .propertyIsEnumerable() to get the DontEnum flag.

    what would be a better/alternative implementations of initializing an object based on on a set of passed properties without utilizing the second parameter?

    Use an extend function. Example with jQuery:

    var instance = $.extend(Object.create(template), {options:arrayOfOptions});
    

    Example with self-made helper function:

    function createAndExtend(proto, props) {
        var o = Object.create(proto);
        for (var p in props)
            o[p] = props[p];
        return o;
    }
    var instance = createAndExtend(template, {options:arrayOfOptions});
    

    You hardly ever need property descriptors anyway.

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