1.let
1.
不存在变量提升和暂时性死区
// 2.
不存在变量提升和暂时性死区
:var
声明之前使用,是
undefined,let
声明之前使用报错
console
.
log
(
three
);
//Cannot access 'three' before initialization
let
three
=
30
;
块级作用域 :
// 4.let
在块级作用域中起作用
if
(
true
) {
var
four
=
20
;
//
全局变量
}
console
.
log
(
four
);
//20
if
(
true
) {
let
five
=
30
;
}
console
.
log
(
five
);
//five is not defined
// 6.let
配合
DOM
绑定事件,可以不用考虑下标绑定到
this
let
btns
=
document
.
querySelectorAll
(
"button"
)
let
boxs
=
document
.
querySelectorAll
(
".box"
)
for
(
let
i
=
0
;
i
<
btns
.
length
;
i
++
) {
btns
[
i
].
onclick
=
function
() {
console
.
log
(
i
);
for
(
let
j
=
0
;
j
<
boxs
.
length
;
j
++
) {
boxs
[
j
].
style
.
display
=
"none"
;
}
boxs
[
i
].
style
.
display
=
"block"
}
}