let、const、class声明的变量不属于顶层对象★

const USER_NAME = "wangzz";
class A {}
let age = 25;
var job = "coder";

function printInfo() {
    console.log("USER_NAME: ", this.USER_NAME);
    console.log("age: ", this.age);
    console.log("class A", this.A);
    console.log("job: ", this.job);
}

printInfo()
// USER_NAME:  undefined
// age:  undefined
// class A undefined
// job:  coder

ES6规定,var命令和function命令声明的全局变量,是顶层对象的属性,但是let、const、class命令声明的全局变量不属于顶层对象的属性

posted @ 2024-02-12 23:59  Felix_Openmind  阅读(7)  评论(0编辑  收藏  举报