js的六种继承方式
原型链继承
重点:让新实例的原型等于父类的实例。
特点:1、实例可继承的属性有:实例的构造函数的属性,父类构造函数属性,父类原型的属性。
缺点:1、新实例无法向父类构造函数传参。
2、继承单一。
3、所有新实例都会共享父类实例的属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function Parent(name) { this .name = name || '阿巴' this .say = function () { console.log( this .name); } } Parent.prototype.job = function () { console.log( 'my job is teacher' ); } Parent.prototype.age = 24 function Child() { } Child.prototype = new Parent() let child = new Child() console.log(child.age); console.log(child.name); |
构造函数继承
重点:用.call()和.apply()将父类构造函数引入子类函数
特点:1、只继承了父类构造函数的属性,没有继承父类原型的属性。
2、解决了原型链继承缺点1、2、3。
3、可以继承多个构造函数属性(call多个)。
4、在子实例中可向父实例传参。
缺点:1、只能继承父类构造函数的属性。
2、无法实现构造函数的复用。每次用每次都要重新调用。
3、每个新实例都有父类构造函数的副本,臃肿。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function Parent(name) { this .name = name || '阿巴' this .say = function () { console.log( this .name); } } Parent.prototype.job = function () { console.log( 'my job is teacher' ); } Parent.prototype.age = 24 function Child(name) { Parent.call( this ,name) } let child = new Child( 'zzzz' ) let child1 = new Child() // child.name='阿布' console.log(child.name); child1.say() |
组合继承
重点:结合了两种模式的优点,传参和复用
特点:1、可以继承父类原型上的属性,可以传参,可复用。
2、每个新实例引入的构造函数属性是私有的。缺点:调用了两次父类构造函数(耗内存),子类的构造函数会代替原型上的那个父类构造函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 | function Parent(name){ this .name=name this .age=12 } function Child(name){ Parent.call( this ,name) } Child.prototype= new Parent() let child= new Child( 'wl' ) console.log(child.name); console.log(child.age); |
原型式继承
重点:用一个函数包装一个对象,然后返回这个函数的调用,这个函数就变成了个可以随意增添属性的实例或对象。object.create()就是这个原理。
特点:类似于复制一个对象,用函数来包装。
缺点:1、所有实例都会继承原型上的属性。
2、无法实现复用。(新实例属性都是后面添加的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function createObj(o){ function F(){} F.prototype=o return new F() } let person={ name: 'llm' , age:23, say: function (){ console.log( 'AAAAA' ); } } let person1=createObj(person) let person2=createObj(person) console.log(person1.say()); person2.age=24 console.log(person2.age); console.log(person1.age); |
寄生式继承
重点:用一个函数包装一个对象,然后返回这个函数的调用,这个函数就变成了个可以随意增添属性的实例或对象。object.create()就是这个原理。
特点:类似于复制一个对象,用函数来包装。
缺点:1、所有实例都会继承原型上的属性。
2、无法实现复用。(新实例属性都是后面添加的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function createObj(o){ function F(){} F.prototype=o return new F() } let person={ name: 'llm' , age:23, say: function (){ console.log( 'AAAAA' ); } } let person1=createObj(person) let person2=createObj(person) console.log(person1.say()); person2.age=24 console.log(person2.age); console.log(person1.age); |
寄生组合式继承
重点:修复了组合继承的问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | function Parent(name){ this .name=name; this .colors=[ 'red' , 'blue' , 'green' ] this .age=0 } Parent.prototype.getName= function (){ console.log( this .name); } function Child(name,age){ Parent.call( this ,name); this .age=age; this .name=name; } // 关键的三步 // let F=function(){} // F.prototype=Parent.prototype // Child.prototype=new F() // 封装一下 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); let child1= new Child( 'lucky' ,18) let child2= new Child( 'lucky' ) console.log(child1,); console.log(child2); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通