<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
</html>
<script>
var total = 0;
for (var i = 0; i < 10; i++) {
setTimeout(function () {
total += i;
}, 1);
}
// total的值为100
let total2 = 0;
for (let j = 0; j < 10; j++) {
setTimeout(function () {
total2 += j;
}, 1);
}
// total的值为45
const total3 = 0;
for (let k = 0; k < 10; k++) {
setTimeout(function () {
total3 += k;
}, 1);
}
// total的值为0
</script>
前端面试题——var和let的作用域
最新推荐文章于 2024-11-06 22:41:26 发布