diff --git a/docs/class.md b/docs/class.md index fb91cb0..239ece0 100644 --- a/docs/class.md +++ b/docs/class.md @@ -694,6 +694,20 @@ customName(); // 'foo' 上面代码的import命令,可以用任意名称指向输出的匿名函数。需要注意的是,这时import命令后面,不使用大括号。 +```javascript + +// 第一组 +import crc32 from 'crc32'; +export default function crc32(){} + +// 第二组 +import { crc32 } from 'crc32'; +export function crc32(){}; + +``` + +上面代码的两组写法,第一组是使用export default时,对应的import语句不需要使用大括号;第二组是不使用export default时,对应的import语句需要使用大括号。 + export default命令用在非匿名函数前,也是可以的。 ```javascript diff --git a/docs/function.md b/docs/function.md index ecf8f7d..1632c0f 100644 --- a/docs/function.md +++ b/docs/function.md @@ -28,13 +28,11 @@ if (typeof y === 'undefined') { y = 'World'; } - // 写法二 if (arguments.length === 1) { y = 'World'; } - ``` ES6允许为函数的参数设置默认值,即直接写在参数定义的后面。 @@ -65,6 +63,30 @@ var p = new Point(); ``` +默认值的写法非常灵活,下面是一个为对象属性设置默认值的例子。 + +```javascript + +fetch(url, { body='', method='GET', headers={} }){ + console.log(method); +} + +``` + +上面代码中,传入函数fetch的第二个参数是一个对象,调用的时候可以为它的三个属性设置默认值。这个例子也说明,不仅函数定义时,可以设置参数默认值,而且函数调用时,也可以设置参数默认值。 + +甚至还可以设置双重默认值。 + +```javascript + +fetch(url, { method='GET' } = {}){ + console.log(method); +} + +``` + +上面代码中,调用函数fetch时,如果不含第二个参数,则默认值为一个空对象;如果包含第二个参数,则它的method属性默认值为GET。 + 定义了默认值的参数,必须是函数的尾部参数,其后不能再有其他无默认值的参数。这是因为有了默认值以后,该参数可以省略,只有位于尾部,才可能判断出到底省略了哪些参数。 ```javascript @@ -111,11 +133,11 @@ foo(undefined, null) ```javascript function throwIfMissing() { - throw new Error('Missing parameter'); + throw new Error('Missing parameter'); } function foo(mustBeProvided = throwIfMissing()) { - return mustBeProvided; + return mustBeProvided; } foo() diff --git a/docs/reference.md b/docs/reference.md index 165cbf7..ac069ee 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -20,7 +20,7 @@ - Traceur-compiler, [Language Features](https://github.com/google/traceur-compiler/wiki/LanguageFeatures): Traceur文档列出的一些ES6例子 - Axel Rauschmayer, [ECMAScript 6: what’s next for JavaScript?](https://speakerdeck.com/rauschma/ecmascript-6-whats-next-for-javascript-august-2014): 关于ES6新增语法的综合介绍,有很多例子 - Toby Ho, [ES6 in io.js](http://davidwalsh.name/es6-io) - +- Guillermo Rauch, [ECMAScript 6](http://rauchg.com/2015/ecmascript-6/) ## 语法点 @@ -104,3 +104,4 @@ - esnext, [ES6 Module Transpiler](https://github.com/esnext/es6-module-transpiler):基于node.js的将ES6模块转为ES5代码的命令行工具 - Sebastian McKenzie, [6to5](https://github.com/sebmck/6to5): 将ES6转为ES5代码的Node模块,支持source map - SystemJS, [SystemJS](https://github.com/systemjs/systemjs): 在浏览器中加载AMD、CJS、ES6模块的一个垫片库 +- Facebook, [regenerator](https://github.com/facebook/regenerator): 将Generator函数转为ES5的转码器