gpt4 book ai didi

javascript - 为什么我不能删除 document.body 属性?

转载 作者:行者123 更新时间:2023-11-30 16:31:14 27 4
gpt4 key购买 nike

> delete document
false

这个我有点理解:document是window的一个不可配置的属性。

> delete document.body
true
> document.body
<body>
...</body>

但这是什么巫术?

最佳答案

因为文档没有“正文”属性。或者更确切地说,它没有自己的属性(property)。

console.log(document.hasOwnProperty("body")); //false
//now let's mimic what we're seeing with document.body
function X(){

}

X.prototype.body = "Abc";

var foo = new X();

console.log(foo.body); //Abc
delete foo.body; //no effect because I don't have this property. My prototype does

console.log(foo.body); //Abc (still)

delete foo.__proto__.body; //delete the prototype's property

console.log(foo.body); //undefined (now)

delete document.__proto__.__proto__.body; //delete the doc
console.log(document.body); //undefined (now)

关于javascript - 为什么我不能删除 document.body 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33293845/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com