I am trying to remove a property from an Person
object like this:
const Person = {
firstname: 'John',
lastname: 'Doe'
}
console.log(Person.firstname);
// Output: "John"
delete Person.firstname;
console.log(Person.firstname);
// Output: undefined
When I am using this delete
operator is working fine and Person.firstname
log is showing as undefined
as expected. But when I create a new object using this Person
object using Object.create()
method like this:
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"
You can see Person2.firstname
is returning "John" in the end, when I was expecting it to work same way as done in the first snippet and return undefined
.
So, my questions here are:
- Why is
delete Person2.firstname
not working? - Also, how can we delete
firstname
property from thePerson2
object?
Thanks for your help.