Is a let variable equal to a var variable?
本问题已经有最佳答案,请猛点这里访问。
如果我有类似的东西:
1 2 | let x = 20; var z = 20; |
将
1 | x === z |
这些变量如何声明并不重要,只是它们都在范围内,并且在评估
试试看你自己…
(如果没有显示任何内容,这是因为您使用的浏览器不支持
1 2 3 4 5 6 | "use strict"; let x = 20; var z = 20; document.write(x === z); |
请阅读此答案,了解有关
The difference is scoping.
var is scoped to the nearest function block (or global if outside a function block), andlet is scoped to the nearest enclosing block (or global if outside any block), which can be smaller than a function block.