码迷,mamicode.com
首页 > 其他好文 > 详细

20.Class的继承

时间:2017-06-26 18:03:53      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:cto   tor   const   简介   color   ons   ext   类的构造函数   this   

 

1.简介

Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多

class Point {
}

class ColorPoint extends Point {
}

上面代码定义了一个ColorPoint类,该类通过extends关键字,继承了Point类的所有属性和方法。但是由于没有部署任何代码,所以这两个类完全一样,等于复制了一个Point类。下面,我们在ColorPoint内部加上代码。

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方法和toString方法之中,都出现了super关键字,它在这里表示父类的构造函数,用来新建父类的this对象。

 

 

2.Object.getPrototypeOf()

 

3.super关键字

 

4.类的prototype属性和__proto__属性

 

5.原生构造函数的继承

 

6.Mixin模式的实现

 

20.Class的继承

标签:cto   tor   const   简介   color   ons   ext   类的构造函数   this   

原文地址:http://www.cnblogs.com/miangao/p/7081625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!