ghostgril

i 请教一下看过 vue 源码的同学们

  •  
  •   ghostgril · May 6, 2018 · 1622 views
    This topic created in 2951 days ago, the information mentioned may be changed or developed.

    这个 vue 源码里 init.js resolveConstructorOptions(vm.constructor) 为何要在 constructor 上赋值 super

    1 replies    2018-05-06 22:01:03 +08:00
    noe132
        1
    noe132  
       May 6, 2018   ❤️ 1
    没仔细看 vue 源码,不过可以大概猜一猜
    因为之前的单纯 prototype 实现的子类,实际上只继承了 prototype 的方法和属性,不像 es class,父类构造函数是没有继承的。
    那么想实现一个可以继承父类构造函数的子类,通常的做法就是将父类的构造函数设置为子类的某个属性,在子类的构造函数运行时,先用父类构造函数修饰实例对象。

    vue 这个看起来是只继承了父类的构造函数参数,子类创建的时候把父类的构造函数参数 merge 到子类参数上。

    类似的可以参考 node 中的 util.inherits 方法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5365 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 08:52 · PVG 16:52 · LAX 01:52 · JFK 04:52
    ♥ Do have faith in what you're doing.