extending Object.create() in javascript

68 views Asked by At

I'm extending Object.create() to take a second argument e.g

if (typeof Object.create !== 'function') {
    Object.create = function (o,arg) {
        function F() {}
        F.prototype = o;
        return new F(arg);
    };
}

//could be multiple of these objects that hold data
var a = {
  b : 2
};

var c = function(data){
  return{
    d : Object.create(data)
  };
};

//create new instance of c object and pass some data
var newObj = function(arg){
  return(Object.create(c(arg)))
}

var e = newObj(a);
e.d.b = 5;
var f = newObj(a);
console.log(e.d.b);
console.log(f.d.b);

I'm just wondering if there are any pitfalls to using Object.create() in this way? I would do some extra checking on the arg argument in the Object.create() function if I was to use this but the main point is to find out if this causes any issues or is overkill etc..

1

There are 1 answers

2
Scott Sauyet On BEST ANSWER

Yes, one very large pitfall: you're stepping on the toes of the definition!

Object.create already takes a second parameter, an object mapping keys to property descriptors.

This one is hard to polyfill in older environments, which is why it's still widely ignored, but when it becomes more ubiquitous, your shim will mismatch the standards pretty badly.