1

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

  • polyfill that was used to retrofit IE8 with Object.create functionality, however this polyfill doesn't support a second parameter
  • shim which requires inclusion of es5-shim

with various trains of thought on how to best accomplish this.

Here's my relevant code I am using:

//json_string variable is passed via php

var arrayOfOptions = jQuery.parseJSON(json_string);

var template = {
     // my instance that I want to initialize based on the properties provided
};

var instance = Object.create(template, {'options':{value:arrayOfOptions, enumerable: true}});

My question is how did programmers accomplish this task prior to ES5, and more specifically what would be a better/alternative implementations of initializing an object based on on a set of passed properties without utilizing the second parameter?

Community
  • 1
  • 1
zoranc
  • 2,410
  • 1
  • 21
  • 34
  • 1
    you could not,expect for native dom objects on ie8. – mpm Mar 11 '14 at 11:57
  • here's a question that covers the enumerable property setting prior to ES5 http://stackoverflow.com/questions/8918030/is-it-possible-to-emulate-non-enumerable-properties – martskins Mar 11 '14 at 12:00

1 Answers1

2

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375