英文:
Deleting the property from an object is not working
问题
我已经定义了一个名为Employee
的对象,其中有一个名为company
的属性。在创建了一个新对象Emp1
之后,我尝试删除该对象的属性,但它仍然存在于Emp1
对象下。这是什么原因造成的?
var Employee = {
company: 'xyz'
}
var Emp1 = Object.create(Employee);
delete Emp1.company;
console.log(Emp1.company);
英文:
I have defined an object Employee
with a property name company
. After creating a new object Emp1
I tried to delete the property from the object but it still exists under the object Emp1
. What is the reason for it?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var Employee = {
company: 'xyz'
}
var Emp1 = Object.create(Employee);
delete Emp1.company;
console.log(Emp1.company);
<!-- end snippet -->
答案1
得分: 3
你正在使用 Object.create 来创建一个对象 Emp1,该对象的原型对象具有属性 Employee。因此,该属性不是 Emp1 的一部分,而是原型对象 Employee 的一部分,因此在 Emp1 上删除它不会产生任何影响。
请自行尝试:如果您将属性添加到 Emp1 对象,它将一直存在,直到您删除它:
let Employee = {company: 'xyz'};
let Emp1 = Object.create(Employee);
Emp1.company = 'Emp1s company';
console.log(Emp1.company); // -> 'Emp1s company' (property from Emp1)
delete Emp1.company;
console.log(Emp1.company) // -> 'xyz' (the value from the prototype)
这就是JS中原型继承的工作方式。只要存在一个属性,它就会遮蔽相同原型属性,如果您删除它,将从原型获取相同属性(如果原型不存在,则从原型的原型等获取)。
(因此 Object.create 不会创建副本 - 它创建一个以另一个对象为原型的对象。)
英文:
You are using Object.create to create an object Emp1 with the object that has the property Employee* as its prototype. So the property is not a part of your Emp1, its part of the prototype object Employee and thus deleting it on Emp1 has no effect.
Try it out yourself: If you added the property to your Emp1 object it would exist until you deleted it:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let Employee = {company: 'xyz'};
let Emp1 = Object.create(Employee);
Emp1.company = 'Emp1s company';
console.log(Emp1.company); // -> 'Emp1s company' (property from Emp1)
delete Emp1.company;
console.log(Emp1.company) // -> 'xyz' (the value from the prototype)
<!-- end snippet -->
This is how prototypical inheritance works in JS. A property shadows the same prototype property as long as it exist, if you remove it you get the same property from the prototype if it exists (if not from the prototypes prototype etc.)
(So Object.create is not making a copy - it's creating an object with another object as its prototype.)
答案2
得分: 1
从MDN
> Object.create() 静态方法创建一个新对象,使用现有对象作为新创建对象的原型。
还有
> delete 只对 自有属性 有效。如果对象的原型链上存在同名属性,则 删除后对象将使用原型链上的属性。
因此,Emp1.company
在创建后是对 Employee.prototype.company
的一个 引用,因此无法被删除。如果你想创建 Employee
的一个副本,可以使用 {...Employee}
。现在你可以从副本中删除 company
。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const Employee = { company: "xyz" };
const Emp1 = {...Employee };
delete Emp1.company;
console.log(Emp1.company);
// 使用 Object.create ...
const Emp2 = Object.create(Employee);
// 这里你覆盖了 Employee.prototype.company
// 使用本地属性(首先被检索)
Emp2.company = `abc`;
console.log(Emp2.company, Employee.company);
// 移除本地属性
delete Emp2.company;
// 现在 Emp2.company 再次在其原型链中找到 `company`
console.log(Emp2.company, Employee.company);
<!-- end snippet -->
英文:
From MDN
> The Object.create() static method creates a new object, using an
> existing object as the prototype of the newly created object.
Also
> delete only has an effect on own properties. If a property with the
> same name exists on the object's prototype chain, then after deletion the object will use the property from the prototype chain.
So Emp1.company
is, after creation, a reference to Employee.prototype.company
and thus can't be deleted. If you want to create a copy of Employee
, use {...Employee}
. Now you can delete company
from it.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const Employee = { company: "xyz" };
const Emp1 = {...Employee };
delete Emp1.company;
console.log(Emp1.company);
// with Object.create ...
const Emp2 = Object.create(Employee);
// here you override Employee.prototype.company
// with a local property (which is retrieved
// first)
Emp2.company = `abc`;
console.log(Emp2.company, Employee.company);
// remove the local property
delete Emp2.company;
// now Emp2.company finds `company` in its prototype again
console.log(Emp2.company, Employee.company);
<!-- end snippet -->
答案3
得分: 0
从原型中删除该属性,如下所示:
delete Object.getPrototypeOf(Emp1).company
英文:
You have to delete this property from the prototype like this:
delete Object.getPrototypeOf(Emp1).company
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论