澄清 javascript 无法删除继承的属性. [英] Clarification on the inability of javascript deleting inherited properties.

查看:35
本文介绍了澄清 javascript 无法删除继承的属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们.我正在研究对象的属性,有一件事引起了我的注意.整个文档中的这一部分说明了有关 JS 的内容.

原型属性JavaScript 对象继承其原型的属性.

delete 关键字不会删除继承的属性,但如果删除原型属性,则会影响从原型继承的所有对象.

我有点迷失在这里...我知道这听起来很愚蠢,但我需要确切了解这在可能会发挥作用的流程和应用程序中意味着什么.

解决方案

delete 从对象中删除属性.如果对象继承该属性而不是拥有自己的具有该名称的属性,则对该属性调用 delete 不会执行任何操作:您无法删除不存在的内容在那里.:-) 拥有属性的是对象的原型(或者它的原型,或者它的原型的原型等等),而不是继承它的对象.

一个例子可能会有所帮助.考虑:

//用作原型的对象var p = {答案:42};//一个使用 `p` 作为其原型的对象var o = Object.create(p);console.log(p.answer);//42console.log(p.hasOwnProperty("answer"));//真的console.log(o.answer);//42console.log(o.hasOwnProperty("answer"));//假

p 有属性,而不是 oo 只是继承了它.像这样:

<预>+−−−−−−−−−−−−−−−−−+p−−−−−−−−−−−−−−−−−−−−−−−−+−>|(对象) ||+−−−−−−−−−−−−−−−−−+||[[原型]] |−−−>(Object.prototype)||答案:42 |+−−−−−−−−−−−−−−−−−+ |+−−−−−−−−−−−−−−−−−+o--->|(对象) ||+−−−−−−−−−−−−−−−−−+ ||[[原型]] |−++−−−−−−−−−−−−−−−−−+

所以delete o.answer没有效果;o 没有 answer 属性供 delete 删除.p 是带有 answer 的对象.

如果我们从 p 中删除属性(delete p.answer;),这将删除它 —来自 p.由于原型继承是对象与其原型之间的实时连接,在这样做之后向 o 询问 answer 将给我们 undefined,因为 o(有效地)向 p 请求它,而 p 不再拥有它:

//用作原型的对象var p = {答案:42};//一个使用 `p` 作为其原型的对象var o = Object.create(p);console.log(p.answer);//42console.log(p.hasOwnProperty("answer"));//真的console.log(o.answer);//42console.log(o.hasOwnProperty("answer"));//错误的删除o.answer;//没有效果console.log(p.answer);//42console.log(o.answer);//42删除 p.answer;//从 p 中删除它console.log(p.answer);//不明确的console.log(o.answer);//未定义

.as-console-wrapper {最大高度:100%!重要;}

guys. I'm studying up on properties for objects and one thing caught my eye on a source of info. There was this one part of the whole document that stated this about JS.

Prototype Properties JavaScript objects inherit the properties of their prototype.

The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype.

I'm kinda lost here... I know that sounds dumb but I need to understand exactly what that means during processes and applications where that might come to play.

解决方案

delete removes a property from an object. If the object inherits the property rather than having its own property with that name, calling delete on the property doesn't do anything: You can't remove something that isn't there. :-) It's the object's prototype (or its prototype, or its prototype's prototype, etc.) that has the property, not the object inheriting it.

An example would probably help. Consider:

// An object to use as a prototype
var p = {answer: 42};
// An object using `p` as its prototype
var o = Object.create(p);

console.log(p.answer);                   // 42
console.log(p.hasOwnProperty("answer")); // true
console.log(o.answer);                   // 42
console.log(o.hasOwnProperty("answer")); // false

p has the property, not o; o just inherits it. Like this:

                          +−−−−−−−−−−−−−−−+
p−−−−−−−−−−−−−−−−−−−−−−+−>|   (object)    |
                       |  +−−−−−−−−−−−−−−−+
                       |  | [[prototype]] |−−−>(Object.prototype)
                       |  | answer: 42    | 
     +−−−−−−−−−−−−−−−+ |  +−−−−−−−−−−−−−−−+
o−−−>|    (object)   | |
     +−−−−−−−−−−−−−−−+ |
     | [[Prototype]] |−+
     +−−−−−−−−−−−−−−−+

So delete o.answer has no effect; o has no answer property for delete to remove. p is the object with answer.

If we remove the property from p (delete p.answer;), that will remove it — from p. And since prototypical inheritance is a live connection between an object and its prototype, asking o for answer after doing that will give us undefined, since o (effectively) asks p for it, and p doesn't have it anymore:

// An object to use as a prototype
var p = {answer: 42};
// An object using `p` as its prototype
var o = Object.create(p);

console.log(p.answer);                   // 42
console.log(p.hasOwnProperty("answer")); // true
console.log(o.answer);                   // 42
console.log(o.hasOwnProperty("answer")); // false

delete o.answer;                         // No effect
console.log(p.answer);                   // 42
console.log(o.answer);                   // 42

delete p.answer;                         // Removes it from p
console.log(p.answer);                   // undefined
console.log(o.answer);                   // undefined

.as-console-wrapper {
  max-height: 100% !important;
}

这篇关于澄清 javascript 无法删除继承的属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆