1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-25 11:12:21 +00:00
es6tutorial/docs/class.md
2014-04-25 09:10:39 +08:00

44 lines
857 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# class
ES6引入了Class这个概念可以定义class作为对象的模板。
```javascript
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '('+this.x+', '+this.y+')';
}
}
```
上面代码定义了一个class类可以看到里面有一个constructor函数这就是构造函数。而this关键字则代表实例对象。
class之间可以通过extends关键字实现继承。
```javascript
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y); // same as super.constructor(x, y)
this.color = color;
}
toString() {
return this.color+' '+super();
}
}
```
上面代码定义了一个ColorPoint类该类通过extends关键字继承了Point类的所有属性和方法。在constructor方法内super就指代父类Point。