我正在尝试从这样的Person
对象中删除属性:
const Person = {
firstname: 'John',
lastname: 'Doe'
}
console.log(Person.firstname);
// Output: "John"
delete Person.firstname;
console.log(Person.firstname);
// Output: undefined
当我使用此delete
运算符时,它工作正常,并且Person.firstname
日志undefined
按预期显示。但是当我使用这样的方法使用这个Person
对象创建一个新对象时Object.create()
:
const Person = {
firstname: 'John',
lastname: 'Doe'
}
const Person2 = Object.create(Person);
console.log(Person2.firstname);
// Output: "John"
delete Person2.firstname;
console.log(Person2.firstname);
// expected output: undefined
// actual output: "John"
您可以看到Person2.firstname
最终返回“John”,而我希望它的工作方式与第一个片段中的相同,并返回undefined
。
所以,我的问题是:
- 为什么
delete Person2.firstname
不工作? - 另外,我们如何
firstname
从Person2
对象中删除属性?
谢谢你的帮助。