Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 10.JavaScript 继承的方式和优缺点 #179

Open
qiilee opened this issue Sep 29, 2019 · 0 comments
Open

[js] 10.JavaScript 继承的方式和优缺点 #179

qiilee opened this issue Sep 29, 2019 · 0 comments
Labels

Comments

@qiilee
Copy link
Member

qiilee commented Sep 29, 2019

答案:六种方式

  • 一、原型链继承

    • 缺点:
    • 1.引用类型的属性被所有实例共享
    • 2.在创建 Child 的实例时,不能向 Parent 传参
  • 二、借用构造函数(经典继承)

    • 优点:
    • 1.避免了引用类型的属性被所有实例共享
    • 2.可以在 Child 中向 Parent 传参
    • 缺点:
    • 1.方法都在构造函数中定义,每次创建实例都会创建一遍方法。
  • 三、组合继承

    • 优点:
    • 1.融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。
  • 四、原型式继承

    • 缺点:
    • 1.包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。
  • 五、寄生式继承

    • 缺点:
    • 1.跟借用构造函数模式一样,每次创建对象都会创建一遍方法。
  • 六、寄生组合式继承

    • 优点:
    • 1.这种方式的高效率体现它只调用了一次 Parent 构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。
    • 2.与此同时,原型链还能保持不变;
    • 3.因此,还能够正常使用 instanceof 和 isPrototypeOf。
    • 开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式

解析:参考

@qiilee qiilee added the JS label Sep 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant