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

857 B
Raw Blame History

class

ES6引入了Class这个概念可以定义class作为对象的模板。


class Point {

  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '('+this.x+', '+this.y+')';
  }

}

上面代码定义了一个class类可以看到里面有一个constructor函数这就是构造函数。而this关键字则代表实例对象。

class之间可以通过extends关键字实现继承。


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。