var name = 'John';
console.log(this.name, document.name, window.name, name);
const meme = "Bruce";
console.log(this.meme, document.meme, window.meme, meme);
Run Code Online (Sandbox Code Playgroud)
输出:
John undefined John John
undefined undefined undefined "Bruce"
Run Code Online (Sandbox Code Playgroud)
是否为var和const定义了全局范围?我认为唯一的区别是const是不可变的.
是的, window使用定义的范围和变量与使用和var声明的变量的范围不同。constlet
另请参见是否可以删除使用 const 声明的变量?,为什么链接到 Promise.resolve() 的 .then() 允许重新分配 const 声明?
| 归档时间: |
|
| 查看次数: |
109 次 |
| 最近记录: |