简体   繁体   English

在Javascript中对父属性执行`delete`

[英]`delete` on parent property in Javascript

I was wondering what happens when you (possibly accidentally) delete a parent property (ie one from the prototype chain). 我想知道当您(可能意外地)删除父属性(即原型链中的一个)时会发生什么。 Will delete just fail or will it remove the property from the prototype? 删除会失败还是会从原型中删除属性? What I mean is something like: 我的意思是:

var pro = { "a": 1 };
var obj = Object.create(pro);
delete obj["a"]; // <- what will happen here?
delete obj.a; // <- or here? any difference to the one above

delete obj[a] will return an error; 删除obj [a]将返回错误; delete obj.a will return true, and nothing will happens 删除obj.a将返回true,并且什么也不会发生

if you change obj.a = 2; 如果更改obj.a = 2; and then delete obj.a, it will reset the value to the initial one (obj.a == 1); 然后删除obj.a,它会将值重置为初始值(obj.a == 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM