美文网首页
es6中的类继承

es6中的类继承

作者: 韩宏轩 | 来源:发表于2017-09-12 19:14 被阅读0次

    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

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

    相关文章

      网友评论

          本文标题:es6中的类继承

          本文链接:https://www.haomeiwen.com/subject/mmqhsxtx.html