使用const vs var的Javascript全局作用域

时间:2017-10-02 06:07:58

标签: javascript

var name = 'John';

console.log(this.name, document.name, window.name, name);

const meme = "Bruce";

console.log(this.meme, document.meme, window.meme, meme);

输出:

John undefined John John
undefined undefined undefined "Bruce"

是否为var和const定义了全局范围?我认为唯一的区别是const是不可变的。

1 个答案:

答案 0 :(得分:0)

是的,使用var定义的const范围和变量与使用letabcd@gmail.com,pqrs@gmail.com声明的变量范围不同。

另请参阅Is it possible to delete a variable declared using const?Why does .then() chained to Promise.resolve() allow const declaration to be reassigned?

相关问题