webveuje/es6/bianliang.html
2021-03-23 10:58:10 +08:00

125 lines
2.6 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>
<script>
{
let a = 1;
var b = 2;
a = 9;
let a = "llp"
}
// console.log(a)
// console.log(b)
for (var i = 0; i < 5; i++) {
console.log(i)
}
console.log(i) //5
for (let m = 0; m < 5; m++) {
console.log(m, "llk")
}
// console.log(m)
var a = [];
for (var i = 0; i < 10; i++) {
a[i] = function () {
console.log(i);
};
}
a[6](); // 10
var a = [];
for (let i = 0; i < 10; i++) {
a[i] = function () {
console.log(i);
};
}
a[6](); // 10
const c = 10
c = 9;
console.log(d)
var d = 7
// let aa = 1;
// let bb = 2;
// let cc = 3;
// |
let [aa, bb, cc] = ["a", "b", "c"] //解构赋值
let [foo, [[bar], baz]] = [1, [[2], 3]];
// foo 1
// bar 2
// baz 3
let [, , third] = ["foo", "bar", "baz"];
third // "baz"
let [x, , y] = [1, 2, 3];
x //1
y //3
let [head, ...tail] = [1, 2, 3, 4];
head // 1
tail // [2,3,4]
let [x, y, ...z] = ['a'];
x // a
y // undefined
z // []
// ...先生成一个数组,把后面所有的元素都塞进去
let [a, [b], d] = [1, [2, 3], 4];
a // 1
b // 2
d // 4
// 不完全解构的时候 左边的变量名只能匹配到第一个值作为他的变量值
// 报错
// let [foo] = 1;
// let [foo] = false;
// let [foo] = NaN;
// let [foo] = undefined;
// let [foo] = null;
// let [foo] = {};
// 如果等号的右边不是数组或者严格地说不是可遍历的结构参见《Iterator》一章那么将会报错。
</script>
</head>
<body>
结论 用Let声明的变量只在当前代码块中能够访问
用var声明的变量在代码块内部外部都能访问
在同一个代码块中let 不能重复定义
let 不存在var的变量提升
<p>
const 声明的是常量, 定义完成以后值不能被修改 所以在定义的时候需要给他赋初始值
</p>
</body>
</html>