V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
johnnyNg
V2EX  ›  JavaScript

js 寄生组合继承

  •  
  •   johnnyNg · 2020-03-10 15:24:38 +08:00 · 2227 次点击
    这是一个创建于 1479 天前的主题,其中的信息可能已经有所发展或是发生改变。
    
    function object(o) {
        function F() {}
        F.prototype = o;
        return new F();
    }
    
    function prototype(child, parent) {
        var prototype = object(parent.prototype);
        prototype.constructor = child;
        child.prototype = prototype;
    }
    
    // 当我们使用的时候:
    prototype(Child, Parent);
    

    这是比较标准的答案 然后我这么写的话有问题吗?我感觉差不多

    function extend(Parent, Child) {
      Child.prototype.__proto__ = Parent.prototype;
    }
    
    10 条回复    2020-03-10 21:32:17 +08:00
    godgc
        1
    godgc  
       2020-03-10 15:29:41 +08:00   ❤️ 1
    ```JavaScript
    function inherit(son, father){
    let prototypeObj = Object.create(father.prototype);
    prototypeObj.constructor = son;
    son.prototype = prototypeObj;
    }
    ```
    godgc
        2
    godgc  
       2020-03-10 15:30:03 +08:00
    语法没出来 凑活看下吧
    chenliangngng
        3
    chenliangngng  
       2020-03-10 18:16:05 +08:00   ❤️ 1
    不要用__proto__这个
    Yumwey
        4
    Yumwey  
       2020-03-10 19:49:05 +08:00   ❤️ 1
    不行,构造函数没指向
    shujun
        5
    shujun  
       2020-03-10 20:08:27 +08:00 via iPhone
    ts 它不香吗?
    rabbbit
        6
    rabbbit  
       2020-03-10 20:32:21 +08:00   ❤️ 1
    我觉着唯一的区别是:
    标准答案那个调用了 prototype 后, child.prototype 上的东西就没了.
    楼主的就没问题
    zhw2590582
        7
    zhw2590582  
       2020-03-10 20:36:50 +08:00   ❤️ 1
    function prototype(child, parent) {
    Object.setPrototypeOf(child.prototype, parent.prototype);
    }
    Zhuzhuchenyan
        8
    Zhuzhuchenyan  
       2020-03-10 20:58:09 +08:00   ❤️ 1
    七楼的方法作为 ES6 之后的方法完美复现了标准答案的所有副作用,这边补充一下:
    function a(){}
    function b(){}
    Object.setPrototypeOf(b.prototype, a.prototype)
    const c = new b();

    c.constructor === b() // true
    c instance of b // true
    c instance of a // true
    ---
    顺带一提,标准答案的 object 方法可以视为当 Object.creat()不可用的时候的一个简单 polyfill,当然它并没有实现所有的 Object.create,仅仅将主要作用实现了。
    ironMan1995
        9
    ironMan1995  
       2020-03-10 21:29:09 +08:00   ❤️ 1
    Child.prototype.__proto__ = Parent.prototype;
    此时 Child 的原型指向构造函数的指针指向 Parent 构造函数而不是 Child
    SoloCompany
        10
    SoloCompany  
       2020-03-10 21:32:17 +08:00   ❤️ 2
    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

    Deprecated

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

    Warning: Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, currently a very slow operation in every browser and JavaScript engine
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5465 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 08:02 · PVG 16:02 · LAX 01:02 · JFK 04:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.