4

MDN has specified a polyfill for Object.create using 1 argument:

if (!Object.create) {
    Object.create = (function(){
        function F(){}

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

But I would like to make use of the second argument and make something like this work in IE < 9:

o = Object.create(Object.prototype);


// Example where we create an object with a couple of sample properties.
// (Note that the second parameter maps keys to *property descriptors*.)
o = Object.create(Object.prototype, {
  // foo is a regular "value property"
  foo: { writable:true, configurable:true, value: "hello" },
  // bar is a getter-and-setter (accessor) property
  bar: {
    configurable: false,
    get: function() { return 10 },
    set: function(value) { console.log("Setting `o.bar` to", value) }
}});

My guess it that there is no solution to this, just as it's impossible to use Object.defineProperty in IE < 9 (except for DOM elements).

So, my question is: Is there any non-hacky solution to recreate this behavior in IE7+8?

And by "hacky" I mean something like this:

var myObject = document.createElement('fake');
Johan
  • 35,120
  • 54
  • 178
  • 293
  • 3
    No solution. It's a definition of object properties that simply doesn't exist in those implementations, aside from what you've already mentioned. –  Jun 02 '13 at 15:31
  • 1
    You can make a 'sham' (or use one that already exists like the in es5-shim https://github.com/kriskowal/es5-shim/blob/master/es5-sham.js ) that allows you to pass the second argument, but it won't actually support anything other than enumerable, writable, configurable values. – kybernetikos Dec 09 '13 at 00:40
  • Although polyfill with second argument exists, e.g. https://gist.github.com/lavoiesl/6642066, IE8- doesn't understand getters and setters. – Victor Feb 26 '14 at 14:59

0 Answers0