6、var和let/const 跟window的关系
var声明的变量会加到GO当中,即是浏览器的顶层全局对象:window对象中。
而let/const声明的变量不会加到GO当中:用 let 和 const 声明的全局变量并没有在全局对象中,只是一个块级作用域(Script)中。那要怎么获取呢?在定义变量的块级作用域中就能获取,既然不属于顶层对象Window,那就不加 window(global),直接访问即可。
var foo = 'var'
console.log(window.foo)//var
let bar = 'let'
const baz = 'const'
console.log(window.bar,window.baz)//undefined,undefined
console.log(bar,baz)//let,const
具体可看该文章:https://blog.csdn.net/qq_38128179/article/details/106895443
var声明的变量会加到GO当中,即是浏览器的顶层全局对象:window对象中。
而let/const声明的变量不会加到GO当中:用 let 和 const 声明的全局变量并没有在全局对象中,只是一个块级作用域(Script)中。那要怎么获取呢?在定义变量的块级作用域中就能获取,既然不属于顶层对象Window,那就不加 window(global),直接访问即可。
var foo = 'var'
console.log(window.foo)//var
let bar = 'let'
const baz = 'const'
console.log(window.bar,window.baz)//undefined,undefined
console.log(bar,baz)//let,const
具体可看该文章:https://blog.csdn.net/qq_38128179/article/details/106895443