Last active
August 30, 2018 02:31
-
-
Save wxingheng/081ab351f1a75c6f9d7813fc0208ae15 to your computer and use it in GitHub Desktop.
ES6 语法
解构赋值 箭头函数 字符串模板 async class ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
**Class** | |
ES5 | |
```language | |
function Point(x, y){ | |
this.x = x; | |
this.y = y; | |
} | |
Point.prototype.toString = function(){} ... | |
``` | |
ES6 | |
```language | |
class Point { | |
constructor(x, y){ | |
this.x = x; | |
this.y = y; | |
} | |
toString() { | |
... | |
} | |
} | |
``` | |
上面代码定义了一个“类”,可以看到里面有一个constructor方法,这就是构造方法,而this关键字则代表实例对象。也就是说,es5 的构造函数Point,对应 es6 的Point类的构造方法。 | |
Point类除了构造方法,还定义了一个toString方法。注意,定义“类”的方法的时候,前面不需要加上function这个关键字,直接把函数定义放进去了就可以了。另外,方法之间不需要逗号分隔,加了会报错。 | |
**async await** | |
async function a(){ | |
await new Promise((resolve) => { | |
setTimeout(() => { | |
resolve('001') | |
}, 3000) | |
}) | |
} | |
a(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment