1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 18:32:22 +00:00

修改let

This commit is contained in:
Ruan Yifeng 2015-02-08 20:48:39 +08:00
parent aa30945f1b
commit 8a223e4413
3 changed files with 105 additions and 4 deletions

View File

@ -375,6 +375,15 @@ var d = new Date(...dateFields);
上面代码从数据库取出一行数据通过扩展运算符直接将其传入构造函数Date。 上面代码从数据库取出一行数据通过扩展运算符直接将其传入构造函数Date。
除了扩展数组,扩展运算符还可以将一个数值扩展成数值。
```javascript
[...5]
// [0, 1, 2, 3, 4, 5]
```
扩展运算符还可以将字符串转为真正的数组。 扩展运算符还可以将字符串转为真正的数组。
```javascript ```javascript

View File

@ -88,6 +88,55 @@ if (1) {
上面代码中由于块级作用域内typeof运行时x还没有声明所以会抛出一个ReferenceError。 上面代码中由于块级作用域内typeof运行时x还没有声明所以会抛出一个ReferenceError。
总之在代码块内使用let命令声明变量之前该变量都是不可用的。这在语法上称为“暂时性死区”temporal dead zone简称TDZ
```javascript
if (true) {
// TDZ开始
tmp = 'abc'; // ReferenceError
console.log(tmp); // ReferenceError
let tmp; // TDZ结束
console.log(tmp); // undefined
tmp = 123;
console.log(tmp); // 123
}
```
上面代码中在let命令声明变量tmp之前都属于变量tmp的“死区”。
有些“死区”比较隐蔽,不太容易发现。
```javascript
function bar(x=y, y=2) {
return [x, y];
}
bar(); // 报错
```
上面代码中调用bar函数之所以报错是因为参数x默认值等于另一个参数y而此时y还没有声明属于”死区“。
```javascript
let foo = 'outer';
function bar(func = x => foo) {
let foo = 'inner';
console.log(func()); // outer
}
bar();
```
上面代码中函数bar的参数func默认是一个匿名函数返回值为foo。这个匿名函数运行时foo只在函数体外声明内层的声明还没执行因此foo指向函数体外的声明输出outer。
注意let不允许在相同作用域内重复声明同一个变量。 注意let不允许在相同作用域内重复声明同一个变量。
```javascript ```javascript
@ -106,6 +155,22 @@ if (1) {
``` ```
因此,不能在函数内部重新声明参数。
```javascript
function func(arg) {
let arg; // 报错
}
function func(arg) {
{
let arg; // 不报错
}
}
```
## 块级作用域 ## 块级作用域
let实际上为JavaScript新增了块级作用域。 let实际上为JavaScript新增了块级作用域。
@ -205,3 +270,28 @@ const message = "Goodbye!";
const age = 30; const age = 30;
``` ```
由于const命令只是指向变量所在的地址所以将一个对象声明为常量必须非常小心。
```javascript
const foo = {};
foo.prop = 123;
foo.prop
// 123
foo = {} // 不起作用
```
上面代码中常量foo储存的是一个地址这个地址指向一个对象。不可变的只是这个地址即不能把foo指向另一个地址但对象本身是可变的所以依然可以为其添加新属性。如果真的想将对象冻结应该使用Object.freeze方法。
```javascript
const foo = Object.freeze({});
foo.prop = 123; // 不起作用
```
上面代码中常量foo指向一个冻结的对象所以添加新属性不起作用。

View File

@ -26,6 +26,7 @@
- Kyle Simpson, [For and against `let`](http://davidwalsh.name/for-and-against-let): 讨论let命令的作用域 - Kyle Simpson, [For and against `let`](http://davidwalsh.name/for-and-against-let): 讨论let命令的作用域
- kangax, [Why `typeof` is no longer “safe”](http://es-discourse.com/t/why-typeof-is-no-longer-safe/15): 讨论在块级作用域内let命令的变量声明和赋值的行为 - kangax, [Why `typeof` is no longer “safe”](http://es-discourse.com/t/why-typeof-is-no-longer-safe/15): 讨论在块级作用域内let命令的变量声明和赋值的行为
- Axel Rauschmayer, [Variables and scoping in ECMAScript 6](http://www.2ality.com/2015/02/es6-scoping.html): 讨论块级作用域与let和const的行为
- Nick Fitzgerald, [Destructuring Assignment in ECMAScript 6](http://fitzgeraldnick.com/weblog/50/): 详细介绍解构赋值的用法 - Nick Fitzgerald, [Destructuring Assignment in ECMAScript 6](http://fitzgeraldnick.com/weblog/50/): 详细介绍解构赋值的用法
- Nicholas C. Zakas, [Understanding ECMAScript 6 arrow functions](http://www.nczonline.net/blog/2013/09/10/understanding-ecmascript-6-arrow-functions/) - Nicholas C. Zakas, [Understanding ECMAScript 6 arrow functions](http://www.nczonline.net/blog/2013/09/10/understanding-ecmascript-6-arrow-functions/)
- Jack Franklin, [Real Life ES6 - Arrow Functions](http://javascriptplayground.com/blog/2014/04/real-life-es6-arrow-fn/) - Jack Franklin, [Real Life ES6 - Arrow Functions](http://javascriptplayground.com/blog/2014/04/real-life-es6-arrow-fn/)
@ -66,6 +67,7 @@
- Jan Krems, [Generators Are Like Arrays](https://gist.github.com/jkrems/04a2b34fb9893e4c2b5c): 讨论Generator可以被当作数据结构看待 - Jan Krems, [Generators Are Like Arrays](https://gist.github.com/jkrems/04a2b34fb9893e4c2b5c): 讨论Generator可以被当作数据结构看待
- Harold Cooper, [Coroutine Event Loops in Javascript](http://syzygy.st/javascript-coroutines/): Generator用于实现状态机 - Harold Cooper, [Coroutine Event Loops in Javascript](http://syzygy.st/javascript-coroutines/): Generator用于实现状态机
- Ruslan Ismagilov, [learn-generators](https://github.com/isRuslan/learn-generators): 编程练习共6道题 - Ruslan Ismagilov, [learn-generators](https://github.com/isRuslan/learn-generators): 编程练习共6道题
- Kyle Simpson, [Iterating ES6 Numbers](http://blog.getify.com/iterating-es6-numbers/): 在数值对象上部署遍历器
## Promise对象 ## Promise对象