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

修改function & class

This commit is contained in:
Ruan Yifeng 2015-02-23 16:52:08 +08:00
parent 262518bc91
commit 0c99976064
3 changed files with 42 additions and 5 deletions

View File

@ -694,6 +694,20 @@ customName(); // 'foo'
上面代码的import命令可以用任意名称指向输出的匿名函数。需要注意的是这时import命令后面不使用大括号。 上面代码的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命令用在非匿名函数前也是可以的。 export default命令用在非匿名函数前也是可以的。
```javascript ```javascript

View File

@ -28,13 +28,11 @@ if (typeof y === 'undefined') {
y = 'World'; y = 'World';
} }
// 写法二 // 写法二
if (arguments.length === 1) { if (arguments.length === 1) {
y = 'World'; y = 'World';
} }
``` ```
ES6允许为函数的参数设置默认值即直接写在参数定义的后面。 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 ```javascript
@ -111,11 +133,11 @@ foo(undefined, null)
```javascript ```javascript
function throwIfMissing() { function throwIfMissing() {
throw new Error('Missing parameter'); throw new Error('Missing parameter');
} }
function foo(mustBeProvided = throwIfMissing()) { function foo(mustBeProvided = throwIfMissing()) {
return mustBeProvided; return mustBeProvided;
} }
foo() foo()

View File

@ -20,7 +20,7 @@
- Traceur-compiler, [Language Features](https://github.com/google/traceur-compiler/wiki/LanguageFeatures): Traceur文档列出的一些ES6例子 - Traceur-compiler, [Language Features](https://github.com/google/traceur-compiler/wiki/LanguageFeatures): Traceur文档列出的一些ES6例子
- Axel Rauschmayer, [ECMAScript 6: whats next for JavaScript?](https://speakerdeck.com/rauschma/ecmascript-6-whats-next-for-javascript-august-2014): 关于ES6新增语法的综合介绍有很多例子 - Axel Rauschmayer, [ECMAScript 6: whats 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) - 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代码的命令行工具 - 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 - Sebastian McKenzie, [6to5](https://github.com/sebmck/6to5): 将ES6转为ES5代码的Node模块支持source map
- SystemJS, [SystemJS](https://github.com/systemjs/systemjs): 在浏览器中加载AMD、CJS、ES6模块的一个垫片库 - SystemJS, [SystemJS](https://github.com/systemjs/systemjs): 在浏览器中加载AMD、CJS、ES6模块的一个垫片库
- Facebook, [regenerator](https://github.com/facebook/regenerator): 将Generator函数转为ES5的转码器