javascript - 为什么将其他对象分配给内置对象(例如文档、窗口)没有意义?

标签 javascript

<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/

相关文章:

javascript - Nodejs Promise.all() 总是解析

javascript - React 组件未显示在浏览器上,出现空白屏幕

javascript - 无法在 PUG/JADE 中显示用户列表

javascript - POST 请求更改 url 并刷新页面

javascript - 如何在 Chrome 扩展中配置 Ember 路由?

javascript - JavaScript 如何确定数组索引是否为整数?

javascript - 使用PHP循环文件上传表单

javascript - 单击按钮时应用 Css

javascript - 查找并替换多个 HTML 标记中的字符

javascript - 由于 div 设置为不显示,排序表交替颜色未显示