1. 原型链继承
function Parent(){
this.name = 'Kevin'
}
Parent.prototype.getName = function(){
consolo.log(this.name)
}
function Child(){
}
Child.prototype = new Parent()
var child1 = new Child()
console.log(child1.getName()) // Kevin
问题:
- 引用类型的属性被所有实例共享,举个例子:
function Parent(){
this.name = ['Kevin','daisy']
}
function Child(){
}
Child.prototype = new Parent();
var chlid1 = new Child()
child1.names.push('Lucy')
console.log(child1.names) // ['Kevin','daisy','Lucy']
var child2 = new Child()
console.log(child2.names) // ['Kevin','daisy','Lucy']
-
在创建Child实例时,不能向Parent传参 重点:让新实例的原型等于父类的实例。
特点:1、实例可继承的属性有:实例的构造函数的属性,父类构造函数属性,父类原型的属性。(新实例不会继承父类实例的属性!) 缺点:1、新实例无法向父类构造函数传参。 2、继承单一。 3、所有新实例都会共享父类实例的属性。(原型上的属性是共享的,一个实例修改了原型属性,另一个实例的原型属性也会被修改!)
2. 构造函数继承(经典继承)
function Parent(){
this.names = ['Kevin','daisy']
}
function Child(name){
Parent.call(this,name)
}
var child1 = new Child()
child1.names.push('Lucy')
console.log(child1.names) // ['Kevin','daisy','Lucy']
var child2 = new Child()
console.log(child2.names) // ['Kevin','daisy']
优点:
- 避免了引用类型的属性被所有实例共享
- 可以在 Child 向 Parent 传参 举个例子:
function Parent(name){
this.name = name
}
function Child(name){
Parent.call(this,name)
}
var child1 = new Child('Kevin')
console.log(child1.name) // Kevin
var child2 = new Child('Lucy')
console.log(child2.name) // Lucy
缺点:
- 方法都在构造函数中定义,每次创建实例都会创建一遍方法。
- 无法继承到超类原型上的属性(即_proto_和prototype)
3. 组合继承
原型链继承+构造函数继承
function Parent(name){
this.name = name
this.colors = ['pink','blue','green']
}
Parent.prototype.getName = function(){
console.log(this.name)
}
function Child(name,age){
Parent.call(this,name)
this.age = age
}
Child.prototype = new Parnet()
Child.prorototype.constructor = Child
var child1 = new Child('Kevin',18)
child1.colors.push('orange')
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["pink", "blue", "green", "orange"]
var child2 = new Child('Lucy',18)
console.log(child2.name) // Lucy
console.log(child2.age) // 18
console.log(child2.colors) // ["pink","blue","green"]
优点:融合原型链继继承和构造函数继承的优点,是 JavaScript 中最常用的继承模式
重点:用一个函数包装一个对象,然后返回这个函数的调用,这个函数就变成了个可以随意增添属性的实例或对象。object.create()就是这个原理。
特点:类似于复制一个对象,用函数来包装。
缺点:1、所有实例都会继承原型上的属性。
2、无法实现复用。(新实例属性都是后面添加的)
4. 原型式继承
function createObj(o){
function F(){}
F.prototype = o
return new F()
}
就是 ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。 缺点:包含应用类型的属性值始终都会共享相应的值,这点跟原型链继承有点相似
var Person = {
name:'kevin',
friends:['Lucy','Jack']
}
var person1 = createObj(Person)
var person2 = createObj(Person)
person1.name = 'person1'
console.log(person1.name) // Kevin
person1.friends.push('Kelly')
console.log(person2.friends) // ['Lucy','Jack','Kelly']
注意:修改 person1.name 的值,person2.name 的值并未发生改变,并不是因为 person1 和 person2 有独立的 name 值,而是因为 person1.name = 'person1' ,给 person1 增加了 name 值,并非修改了原型上的 name 值。
重点:用一个函数包装一个对象,然后返回这个函数的调用,这个函数就变成了个可以随意增添属性的实例或对象。object.create()就是这个原理。
特点:类似于复制一个对象,用函数来包装。
缺点:1、所有实例都会继承原型上的属性。
2、无法实现复用。(新实例属性都是后面添加的
5. 寄生式继承
创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。
function createObj(o){
var clone = Object.create(o)
clone.sayName = function(){
console.log('Hi')
}
return clone
}
缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。
重点:就是给原型式继承外面套了个壳子。
优点:没有创建自定义类型,因为只是套了个壳子返回对象(这个),这个函数顺理成章就成了创建的新对象。
缺点:没用到原型,无法复用。
6. 寄生组合式继承
为了方便大家阅读,在这里重复一下组合继承的代码:
function Parent(name){
this.name = name
this.colors = ['pink','blue','green']
}
Parent.prototype.getName = function(){
console.log(this.name)
}
function Child(name,age){
Parent.call(this,name)
this.age = age
}
Child.prototype = new Parnet()
Child.prorototype.constructor = Child
var child1 = new Child('Kevin',18)
child1.colors.push('orange')
console.log(child1.name); // kevin
console.log(child1.age); 组合继承最大的缺点是会调用两次父构造函数。
一次是设置子类型实例原型的时候。
// 18
console.log(child1.colors); // ["pink", "blue", "green", "orange"]
var child2 = new Child('Lucy',18)
console.log(child2.name) // Lucy
console.log(child2.age) // 18
console.log(child2.colors) // ["pink","blue","green"]
组合继承最大的缺点是会调用两次父构造函数。
一次是设置子类型实例的原型的时候。
Child.prototype = new Parent();
一次是在创建子类实例的时候
var child1 = new Child('Kevin','18')
回想下 new 的模拟实现,其实在这句中,我们回执行:
Parent.call(this,name)
在这里,我们又调用了一次 Parent 构造函数。
所以,在这个例子中,如果我们打印 child1 对象,我们会发现 Child.prototype 和 child1 都有一个属性为 colors ,属性值为 ['red', 'blue', 'green'] 。
那么我们该如何精益求精,避免这一次重复调用呢?
如果我们不使用 Child.prototype = new Parent() ,而是间接的让 Child.prototype 访问到 Parent.prototype 呢?
看看如何实现:
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child (name, age) {
Parent.call(this, name);
this.age = age;
}
// 关键的三步
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
var child1 = new Child('kevin', '18');
console.log(child1);
最后我们封装一下这个继承方法:
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);
这种方式的高效率体现它只调用了一次 Parent 构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。
重点:修复了组合继承的问题。