MDN pointed out 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 use the second argument and do 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 is that there is no solution for this, just as it is impossible to use Object.defineProperty in IE <9 (except for the DOM elements).
So my question is: is there any non-hacker solution to recreate this behavior in IE7 + 8?
And by “hacks,” I mean something like this:
var myObject = document.createElement('fake');
source share