es6中的类继承

class parent{

constructor(){

this.name = "parent"

}

p_say(){

console.log("hello")

}

}

class kid extends parent{}

通过观察 new kid() , kid.prototype , new parent() , parent.prototype 可以发现:

1) new parent() 与 new kid() 中的属性一样,kid 通过constructor类继承了parent的属性(相当于调用call)

2) parent.prototype 中的方法是 parent 中constructor之外的方法

3) kid.prototype 为空,只有两个默认属性:

__proto__ : 指向parent.prototype,起到继承 parent.prototype的作用(跨了一级)

constructor : 指向 kid


以上是结果,实现过程并不复杂。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容