6种方式对比
方式 | 关键代码 | 特点 | 缺点 |
---|---|---|---|
原型链继承 | Child.prototype = new Parent() | 继承了父类构造函数属性,父类原型的属性(引用类型的属性被所有实例共享) | 1、新实例无法向父类构造函数传参。 2、继承单一。 3、所有新实例都会共享父类实例的属性! |
构造函数继承 | Parent.call(this) | 1、只继承了父类构造函数的属性,没有继承父类原型的属性(避免了引用类型的属性被所有实例共享 )。 2、解决了原型链继承缺点1、2、3。 3、可以继承多个构造函数属性(call多个)。 4、在子实例中可向父实例传参。 | 1、只能继承父类构造函数的属性。 2、无法实现构造函数的复用(每次用每次都要重新调用) 3、每个新实例都有父类构造函数的副本,臃肿。 |
组合继承 (组合原型链继承和借用构造函数继承) | Parent.call(this, name);Child.prototype = new Parent(); | 1、可以继承父类原型上的属性,可以传参,可复用。 2、每个新实例引入的构造函数属性是私有的。 | 调用了两次父类构造函数(耗内存),子类的构造函数会代替原型上的那个父类构造函数 |
原型式继承 | function createObj(o) {function F(){} F.prototype = o;return new F();} | 用一个函数包装一个对象,然后返回这个函数的调用,这个函数就变成了个可以随意增添属性的实例或对象。object.create()就是这个原理。 | 1、所有实例都会继承原型上的属性。 2、无法实现复用。(新实例属性都是后面添加的) |
寄生式继承 | 给原型式继承外面套了个壳子 | 没用到原型,无法复用。 | |
寄生组合式继承(常用) | var F = function () {};F.prototype = Parent.prototype;Child.prototype = new F(); | 修复了组合继承的问题 |
原型链继承
重点:让新实例的原型等于父类的实例。
特点:1、实例可继承的属性有:实例的构造函数的属性,父类构造函数属性,父类原型的属性。(新实例不会继承父类实例的属性!)
缺点:1、新实例无法向父类构造函数传参。
2、继承单一。
3、所有新实例都会共享父类实例的属性。(原型上的属性是共享的,一个实例修改了原型属性,另一个实例的原型属性也会被修改!)
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、每个新实例都有父类构造函数的副本,臃肿。
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、每个新实例引入的构造函数属性是私有的。缺点:调用了两次父类构造函数(耗内存),子类的构造函数会代替原型上的那个父类构造函数。
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、无法实现复用。(新实例属性都是后面添加的)
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);
寄生式继承
重点:就是给原型式继承外面套了个壳子。
优点:没有创建自定义类型,因为只是套了个壳子返回对象(这个),这个函数顺理成章就成了创建的新对象。
缺点:没用到原型,无法复用。
function createObj(o){
let clone=Object.create(o)
clone.sayName=function(){
console.log('hi');
}
return clone
}
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);
寄生组合式继承(常用)
重点:修复了组合继承的问题
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);