js 原型与原型链继承的关系有点没懂 - V2EX
cwz346852114

js 原型与原型链继承的关系有点没懂

  •  
  •   cwz346852114 Dec 31, 2020 2186 views
    This topic created in 1983 days ago, the information mentioned may be changed or developed.

    class Person{

    }

    class Child extends Person{ xxx(){

    } class 中 方法是绑定在该类的原型上 Person 是 Child 的原型 为啥 Person 实例化对象无法调用 Child 上的 XXX 方法 有哪个大佬解释一下 感觉是我没弄懂 继承的问题。。。

    11 replies    2020-12-31 19:37:24 +08:00
    cwz346852114
        1
    cwz346852114  
    OP
       Dec 31, 2020
    111
    cwz346852114
        2
    cwz346852114  
    OP
       Dec 31, 2020
    111222
    cwz346852114
        3
    cwz346852114  
    OP
       Dec 31, 2020
    Object.getPrototypeOf(child)的时候返回的原型是 Person 我在 Person 上面也找到了 xxx 这个方法 为啥父级直接没法调用
    murmur
        4
    murmur  
       Dec 31, 2020
    是我出现幻觉了么,child 继承 person,也应该说 child 的实例调用 person 的方法
    murmur
        5
    murmur  
       Dec 31, 2020
    class Person{
    test1(params) {
    console.log(111);
    }
    }

    class Child extends Person{
    test2(params) {
    console.log(222);
    }
    }

    var c = new Child();

    c.test2();
    c.test1();

    这样是没问题的
    cwz346852114
        6
    cwz346852114  
    OP
       Dec 31, 2020
    @murmur 我的意思是 父级实例化对象 调用子集里面的 xxx 方法
    murmur
        7
    murmur  
       Dec 31, 2020
    @cwz346852114 你去看看面向对象的思路,都是子类访问父类的方法,我还没听说过父类访问子类的方法
    1KN6sAqR0a57no6s
        8
    1KN6sAqR0a57no6s  
       Dec 31, 2020
    但凡你学过一点面向对象编程也问不出这么不着边际的问题,差的知识太多了,先看看基础吧
    zengzizhao
        9
    zengzizhao  
       Dec 31, 2020
    去看看 C++里的面向对象,从内存方面了解一下
    yanguoyu
        10
    yanguoyu  
       Dec 31, 2020
    难道要问的是类似 C++的虚函数,父类指针调用子类方法?
    Zhuzhuchenyan
        11
    Zhuzhuchenyan  
       Dec 31, 2020
    帮你分析下
    > class Person {}
    > class Child extends Person {test(){}}

    > Child.prototype.hasOwnProperty('test')
    True

    > let aChild = new Child()
    > aChild.__proto__ === Child.prototype
    True

    > aChild.__proto__.__proto__ === Person.prototype
    True

    这才是这根原型链的本来面貌,更具体的,对 Person 的实例化对象而言
    > let aPerson = new Person()
    > aPerson.__proto__ === Person.prototype
    True

    aPerson 的原型链中跳过了一个环节,将 Child.prototype 跳过了,从而 aPerson 中的原型链中不可能有 test 方法。
    扩展阅读
    https://developer.mozilla.org/zh-CN/docs/Web/Javascript/Inheritance_and_the_prototype_chain
    About     Help     Advertise     Blog     API     FAQ     Solana     894 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 20:44 PVG 04:44 LAX 13:44 JFK 16:44
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86