# 面向对象

![image-20200818103647069](./assets/image-20200818103647069.png)

# 继承

## super的使用

### 子类调用父类构造器

```javascript
class Father {
  constructor(x, y){
    this.x = x;
    this.y = y;
  }
  
  sum() {
  	return this.x + this.y;  
  }
  
}

class Son extends Father {
  constructor(x, y){
    this.x = x;
    this.y = y;
  }
}

var son = new Son(1, 2);
son.sum();
```

> 异常方法会报错，因为子类没有使用super()调用父类的构造器，但是在sum方法中调用了this关键字获取了父类的属性

应该使用super()调用父类的的构造器

```javascript
class Father {
  constructor(x, y){
    this.x = x;
    this.y = y;
  }
  
  sum() {
  	return this.x + this.y;  
  }
  
}

class Son extends Father {
  constructor(x, y){
    super(x, y); // 调用了父类的构造器
  }
}

var son = new Son(1, 2);
son.sum();
```

### 子类调用父类的构造器super和this

```javascript
class Son extends Father {
  constructor(x, y){
    super(x, y); // 调用了父类的构造器
    this.x = x;
    this.y = y;
  }
}
```

> super的调用必须在this之前



## this关键字

> this指向调用者！！！

![image-20200818110310163](./assets/image-20200818110310163.png)

