2018-10-12 20:35:31 +00:00
|
|
|
|
---
|
|
|
|
|
title: Let and Const
|
2018-11-25 02:08:43 +00:00
|
|
|
|
localeTitle: let 和 const
|
2018-10-12 20:35:31 +00:00
|
|
|
|
---
|
2018-11-25 02:08:43 +00:00
|
|
|
|
## let
|
2018-10-12 20:35:31 +00:00
|
|
|
|
|
2018-11-25 02:08:43 +00:00
|
|
|
|
let类似于var,但是let有作用域。 let只能在定义的块级作用域中访问。
|
2018-10-12 20:35:31 +00:00
|
|
|
|
```
|
|
|
|
|
if (true) {
|
|
|
|
|
let a = 40;
|
|
|
|
|
console.log(a); //40
|
|
|
|
|
}
|
|
|
|
|
console.log(a); // undefined
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在上面的示例中,变量'a'在If语句中定义,因此在函数外部无法访问。
|
|
|
|
|
|
|
|
|
|
另一个例子:
|
|
|
|
|
```
|
|
|
|
|
let a = 50;
|
|
|
|
|
let b = 100;
|
|
|
|
|
if (true) {
|
|
|
|
|
let a = 60;
|
|
|
|
|
var c = 10;
|
|
|
|
|
console.log(a/c); // 6
|
|
|
|
|
console.log(b/c); // 10
|
|
|
|
|
}
|
|
|
|
|
console.log(c); // 10
|
|
|
|
|
console.log(a); // 50
|
|
|
|
|
```
|
|
|
|
|
|
2018-11-25 02:08:43 +00:00
|
|
|
|
## const
|
2018-10-12 20:35:31 +00:00
|
|
|
|
|
2018-11-25 02:08:43 +00:00
|
|
|
|
const声明一个常量。常量的值不能修改。
|
2018-10-12 20:35:31 +00:00
|
|
|
|
```
|
|
|
|
|
const a = 50;
|
|
|
|
|
a = 60; // shows error. You cannot change the value of const.
|
|
|
|
|
const b = "Constant variable";
|
|
|
|
|
b = "Assigning new value"; // shows error.
|
|
|
|
|
```
|
|
|
|
|
|
2018-11-25 02:08:43 +00:00
|
|
|
|
看另一个例子。
|
2018-10-12 20:35:31 +00:00
|
|
|
|
```
|
|
|
|
|
const LANGUAGES = ['Js', 'Ruby', 'Python', 'Go'];
|
|
|
|
|
LANGUAGES = "Javascript"; // shows error.
|
|
|
|
|
LANGUAGES.push('Java'); // Works fine.
|
|
|
|
|
console.log(LANGUAGES); // ['Js', 'Ruby', 'Python', 'Go', 'Java']
|
|
|
|
|
```
|
|
|
|
|
|
2018-11-25 02:08:43 +00:00
|
|
|
|
可能会有点迷惑。
|
2018-10-12 20:35:31 +00:00
|
|
|
|
|
2018-11-25 02:08:43 +00:00
|
|
|
|
换一种方式想。无论何时定义const变量,Javascript都会将值的地址引用给变量。在我们的示例中,变量'LANGUAGES'实际上引用了分配给数组的内存。因此,您无法更改变量以引用其他内存位置。在整个程序中它只引用数组。
|