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');