window中的var和let
不说先看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var web = "这个是var变量";
console.log(window.web);
//var 会使变量提升至window
let car = "这个是let变量";
console.log(window.car);
//let 不会使变量提升
</script>
</body>
</html>
结果
js设计初期就存在变量提升的问题,这时候就需要使用let
- let,const在同一个作用域都有变量重复声明提醒的功能
- let,const不存在变量提升的问题(块级作用域)