我正在尝试编写代码来删除对象中的属性。如果它是继承的,它必须上链并从祖先删除它。 到目前为止,我已经想出了这个(不起作用):
// My objects:
var pro = {'a':1};
var pro2 = Object.create(pro);
var pro3 = Object.create(pro2);
// --------------------------------------------- --------
function deleteProp(obj, prop){
console.log(obj,prop)
//get own properties
var ownprop = Object.getOwnPropertyNames(obj);
for(var i=0 ;i <ownprop.length; i++){
if (prop === ownprop[i]){
delete obj.ownprop[i];
}
else{
//get the parent
var parent = Object.getPrototypeOf(obj);
console.log(parent);
while (Object.getPrototypeOf(parent)!== Object.prototype){
//recursive call
deleteProp(parent, prop);
}
}
}
}