class

基本语法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//ES5
function Point(x,y){
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return '(' + this.x + ', ' + this.y + ')';
};

//ES6
class Point {
  //constructor是类的默认方法,通过new命令生成对象实例时,自动调用该方法
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {//定义在类的prototype属性上面
    return '(' + this.x + ', ' + this.y + ')';
  }
}

class的继承

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class ColorPoint extends Point {
  constructor(x, y, color) {
    super(x, y); // 调用父类的constructor(x, y)
    this.color = color;
  }

  toString() {
    return this.color + ' ' + super.toString(); // 调用父类的toString()
  }
}

子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工