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

add some content

This commit is contained in:
ruanyf 2014-04-25 20:36:00 +08:00
parent f68e86f38b
commit db71d9829c
7 changed files with 64 additions and 3 deletions

View File

@ -84,7 +84,7 @@ body {
#sidebar ol {
margin: 0;
padding-left: 10px;
padding-left: 30px;
padding-top: 0;
}

View File

@ -151,3 +151,30 @@ for (n of fibonacci()) {
```
从上面代码可见使用for...of语句时不需要使用next方法。
## yield*语句
如果yield命令后面跟的是一个遍历器需要在yield命令后面加上星号表明它返回的是一个遍历器。这被称为yield*语句。
```javascript
let delegatedIterator = (function* () {
yield 'Hello!';
yield 'Bye!';
}());
let delegatingIterator = (function* () {
yield 'Greetings!';
yield* delegatedIterator;
yield 'Ok, bye.';
}());
for(let value of delegatingIterator) {
console.log(value);
}
// "Greetings!
// "Hello!"
// "Bye!"
// "Ok, bye."
```

View File

@ -102,6 +102,24 @@ function f1() {
```
另外ES6的函数也默认在块级作用域内声明。
```javascript
function f() { console.log('I am outside!'); }
(function () {
if(false) {
// What should happen with this redeclaration?
function f() { console.log('I am inside!'); }
}
f();
}());
```
上面代码在ES5中运行会得到“I am inside!”但是在ES6中运行会得到“I am outside!”。
## const命令
const也用来声明变量但是声明的是常量。一旦声明常量的值就不能改变。

View File

@ -145,6 +145,16 @@ Object.defineProperty(a, mySymbol, { value: 'Hello!' });
上面代码通过点结构和Object.defineProperty两种方法为对象增加一个属性。
下面的写法为Map结构添加了一个成员但是该成员永远无法被引用。
```javascript
let a = Map();
a.set(Symbol(), 'Noise');
a.size // 1
```
如果要在对象内部使用symbol属性名必须采用属性名表达式。
```javascript

View File

@ -7,6 +7,7 @@
- Dale Schouten, [10 Ecmascript-6 tricks you can perform right now](http://html5hub.com/10-ecmascript-6-tricks-you-can-perform-right-now/)
- Domenic Denicola, [ES6: The Awesome Parts](http://www.slideshare.net/domenicdenicola/es6-the-awesome-parts)
- Nicholas C. Zakas, [Understanding ECMAScript 6](https://github.com/nzakas/understandinges6)
- Justin Drake, [ECMAScript 6 in Node.JS](https://github.com/JustinDrake/node-es6-examples)
- Ryan Dao, [Summary of ECMAScript 6 major features](http://ryandao.net/portal/content/summary-ecmascript-6-major-features)
- Luke Hoban, [ES6 features](https://github.com/lukehoban/es6features)

View File

@ -267,4 +267,4 @@ console.log(value); // undefined
```
WeakMap还有has和delete方法但没有size方法也无法遍历它的值。
WeakMap还有has和delete方法但没有size方法也无法遍历它的值这与WeakMap的键被垃圾回收机制忽略有关

View File

@ -37,5 +37,10 @@
// run
ditto.run();
</script>
<noscript>
<p>《ECMAScript 6入门》是一本开源的JavaScript语言教程全面介绍ECMAScript 6新增的语法特性。</p>
<p>本书力争覆盖ES6与ES5的所有不同之处对涉及的语法知识给予详细介绍并给出大量简洁易懂的示例代码。</p>
<p>本书为中级难度适合已有一定JavaScript语言基础的读者了解这门语言的最新进展也可当作参考手册查寻新增的语法点。</p>
</noscript>
</body>
</html>