FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Learning/ECMAScript6/let.html at master · xiazhe/JavaScript-Learning · GitHub
xiazhe
/
JavaScript-Learning
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
4
Code
Issues
0
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Learning
/
ECMAScript6
/
let.html
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (55 loc) · 1.89 KB
Breadcrumbs
JavaScript-Learning
/
ECMAScript6
/
let.html
Copy path
File metadata and controls
74 lines (55 loc) · 1.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html
>
<
html
>
<
head
>
<
title
>
let和const命令
</
title
>
<
meta
charset
="
utf-8
"
/>
</
head
>
<
body
>
<
script
>
//http://es6.ruanyifeng.com/#docs/let
// 基本用法
{
let
a
=
10
;
var
b
=
1
;
}
//console.log(a);//let.html:15 Uncaught ReferenceError: a is not defined
//console.log(b); //1
// for 循环
var
arr
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
]
;
for
(
let
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
}
//console.log(i); //let.html:22 Uncaught ReferenceError: i is not defined
//http://es6-features.org/#Constants
var
a
=
[
]
;
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
a
[
i
]
=
function
(
)
{
console
.
log
(
i
)
;
}
;
}
a
[
6
]
(
)
;
// 6
// 不存在变量提升, 先声明, 后使用
console
.
log
(
foo
)
;
// 输出undefined
console
.
log
(
bar
)
;
// 报错ReferenceError
var
foo
=
2
;
let
bar
=
2
;
// 暂时性死区 temporal dead zone TDZ
var
tmp
=
123
;
if
(
true
)
{
tmp
=
'abc'
;
// ReferenceError
let
tmp
;
}
// 上面代码中,存在全局变量tmp,但是块级作用域内let又声明了一个局部变量tmp,
// 导致后者绑定这个块级作用域,所以在let声明变量前,对tmp赋值会报错。
// ES6明确规定,如果区块中存在let和const命令,这个区块对这些命令声明的变量,
// 从一开始就形成了封闭作用域。凡是在声明之前就使用这些变量,就会报错。
if
(
true
)
{
// TDZ开始
tmp
=
'abc'
;
// ReferenceError
console
.
log
(
tmp
)
;
// ReferenceError
let
tmp
;
// TDZ结束
console
.
log
(
tmp
)
;
// undefined
tmp
=
123
;
console
.
log
(
tmp
)
;
// 123
}
</
script
>
</
body
>
</
html
>
Back
|
FazBrowse Home
|
New Git URL