• js继承的几种方式(原型链继承、构造函数继承、组合式继承、寄生组合式继承、ES6的Class类继承)


    1.原型链继承

    实现原理:子类的原型指向父类实例。子类在自身实例上找不到属性和方法时去它父类实例(父类实例和实例的原型对象)上查找,从而实现对父类属性和方法的继承

    缺点:

    • 子类创建时不能传参(即没有实现super()的功能);
    • 父类实例的修改会影响子类所有实例
    1. function Parent(name){
    2. this.name = "父级的name";
    3. }
    4. Parent.prototype.getName = function(){
    5. console.log("getName:"+this.name);
    6. }
    7. function Child(){
    8. }
    9. // 子类原型指向父类的实例
    10. Child.prototype = new Parent();
    11. Child.prototype.constructor = Child;//这句话和原型链继承没有关系,只是根据原型链规则绑定constructor
    12. // 测试
    13. var child = new Child();
    14. console.log(child.name);//父级的name
    15. child.getName();//getName:父级的name
    16. // 缺点:不能传参;父类实例改变子类所有实例也改变

    2.构造函数继承

    实现原理:子类构造函数中执行父类的构造函数,并且为父类构造函数绑定子类的this,父类的构造函数把成员属性和方法都挂到子类的this上去,这样既能避免实例之间共享一个原型实例,又能向父类构造方法传参

    缺点:无法继承父类原型上的属性和方法

    1. // 构造函数继承
    2. function Parent(name) {
    3. this.name = name;
    4. console.log("父类构造函数");
    5. }
    6. Parent.prototype.getName = function () {
    7. console.log("getName:" + this.name);
    8. }
    9. function Child(name){
    10. Parent.call(this, name);
    11. }
    12. var child = new Child("张三");
    13. console.log(child);//Child {name: '张三'}
    14. // 不能继承父类原型上的方法和属性
    15. child.getName();//报错,child.getName is not a function

    3.组合式继承

    实现原理:原型链继承+构造函数继承

    缺点:父类构造函数会执行两次(Parent.call()和new Parent()),这不影响子类对父类的继承,但是每次创建子类实例时原型中都会有两份相同的属性和方法

    1. // 组合式继承:原型链继承+构造函数继承
    2. function Parent(name) {
    3. this.name = name;
    4. console.log("父类构造函数");
    5. }
    6. Parent.prototype.getName = function () {
    7. console.log("getName:" + name);
    8. }
    9. function Child(name) {
    10. Parent.call(this, this.name);
    11. }
    12. Child.prototype = new Parent();
    13. Child.prototype.constructor = Child;
    14. var child = new Child("张三");
    15. console.log(child);//Child {name: '张三'}
    16. child.getName();

     

    4.寄生式组合继承

    实现原理:父类构造函数会执行两次(Parent.call()和new Parent()),那么在原型链继承时就只继承父类的原型,就不会执行两次父类构造函数  Child.prototype = Parent.prototype;

    缺点:操作子类原型对象,会影响到父类原型对象,例如给Child.prototype增加一个getName()方法,那么会导致Parent.prototype也增加或被覆盖一个getName()方法

    1. // 寄生式组合继承:原型链继承(只继承父类原型)+构造函数继承
    2. function Parent(name) {
    3. this.name = name;
    4. }
    5. Parent.prototype.getName = function () {
    6. console.log("getName:" + this.name);
    7. }
    8. function Child(name) {
    9. Parent.call(this, name);
    10. }
    11. Child.prototype = Parent.prototype;
    12. Child.prototype.constructor = Child;
    13. var child = new Child("张三");
    14. console.log(child);//Child {name: '张三'}
    15. child.getName();

    4.1解决寄生式组合继承的缺点(使用Object.create()进行继承) 

    如下,对Child.prototype.getName子类中原型上属性或方法进行修改时,父类也被修改

    1. function Parent(name) {
    2. this.name = name;
    3. console.log("父类构造函数");
    4. }
    5. Parent.prototype.getName = function () {
    6. console.log("父类getName");
    7. }
    8. function Child(name) {
    9. Parent.call(this, name);
    10. }
    11. Child.prototype = Parent.prototype;
    12. Child.prototype.constructor = Child;
    13. Child.prototype.getName = function(){
    14. console.log("子类getName");
    15. }
    16. var child = new Child("张三");
    17. console.log(child);//Child {name: '张三'}
    18. child.getName();
    19. var parent = new Parent("李四");
    20. parent.getName();

     

    解决:加上Object.create()方法即可

    1. // Child.prototype = Parent.prototype;
    2. Child.prototype = Object.create(Parent.prototype);

     

    5.ES6的Class继承

    实现原理:ES6新增,是ES5中构造函数+原型链继承组合继承,寄生组合式继承的结合

    缺点:兼容性不好

    6.扩展——对象的几种创建方式

    • 字面量创建
    • var obj = new Object()创建
    • 构造函数创建
    • Object.create()创建
    1. // 字面量创建
    2. let obj1 = {
    3. name: 'lmf1',
    4. say() {
    5. console.log("lmf1 say");
    6. }
    7. }
    8. // new Object()创建
    9. let obj2 = new Object({
    10. name: 'lmf2',
    11. say() {
    12. console.log("lmf2 say");
    13. }
    14. });
    15. // 构造函数创建
    16. function Person(name) {
    17. this.name = name;
    18. }
    19. let obj3 = new Person("lmf3");
    20. Person.say = function () {
    21. console.log("静态方法");
    22. }
    23. Person.prototype.say = function () {
    24. console.log("lmf3 say");
    25. }
    26. // Object.create()创建
    27. let obj4 = Object.create(obj3);
    28. console.log(obj1, obj2, obj3, obj4);

  • 相关阅读:
    基于kafka项目之Keepalived高可用详细介绍
    ZigBee案例笔记 -- IAR for 8051工程创建
    Java面试之数据库面试题
    【机器学习6】概率图模型
    金九银十跳槽季进大厂靠微服务架构实战就够l
    安装与使用ChatTTS文本转语音模型
    十一届财经峰会,群硕凭借掌汇云荣获数字化先锋产品奖
    LuBase 低代码开发框架介绍 - 可私有化部署
    CAN通信----基本原理
    小程序拼团业务,并发支付导致重复给用户返利bug
  • 原文地址:https://blog.csdn.net/qq_34569497/article/details/133855447