<script>
this.document = "xxxx"; // why it doesn't make sense?
console.log(document); // still show document obj in devtools
</script>
我想这可能会被 javascript 引擎禁止。
最佳答案
window.document
不是可写属性。如果您想要一个名为 document
的局部变量,您可以这样做:
(function(){
var document = 'xxxx';
console.log(document);
})();
和:
new function(){
this.document = 'xxxx';
console.log(this.document);
};
两者都会记录“xxxx”
关于javascript - 为什么将其他对象分配给内置对象(例如文档、窗口)没有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18523690/