我正在扩展Object.create()
以接受第二个论点,例如
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);
我只是想知道Object.create()
以这种方式使用是否有任何陷阱?如果我要使用它,我会对函数中的 arg 参数进行一些额外的检查,Object.create()
但重点是找出这是否会导致任何问题或是否过度杀伤等。