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

时间:2015-10-23 02:20:04

标签: javascript html dom

> delete document
  false

我有点理解:文档是窗口的不可配置属性。

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

但这是什么巫术?

1 个答案:

答案 0 :(得分:2)

因为文档没有“body”属性。或者更确切地说,它没有自己的OWN属性。

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)