es5 实现继承的方式

166 阅读2分钟

「这是我参与2022首次更文挑战的第19天,活动详情查看:2022首次更文挑战

继承

原型链继承

function Parent() {
  this.name = 'parent';
}
Parent.prototype.getName = function () {
    console.log(this.name);
}

function Child() {

}
Child.prototype = new Parent();
var child1 = new Child();

console.log(child1.getName()) 

问题:
1.引用类型的属性被所有实例共享

var child1 = new Child();
child1.name = 'yayu';
console.log(child1.name); // yayu

var child2 = new Child();
console.log(child2.name); // yayu

2.在创建 Child 的实例时,不能向Parent传参

经典继承(借用构造函数)

function Parent() {
  this.names = ['kevin', 'daisy'];
}

function Child() {
  Parent.call(this);
}

var child1 = new Child();
child1.names.push('yayu');
console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy"]

优点:

  1. 避免了引用类型的属性被所有实例共享
  2. 可以在 Child 中向 Parent 传参

缺点:

  1. 方法都在构造函数中定义,每次创建实例都会创建一遍方法。
  2. 只能继承构造里的信息

组合继承

原型链继承和经典继承双剑合璧。

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;
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;


var child1 = new Child('kevin', '18');
child1.colors.push('black');
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

var child2 = new Child('daisy', '20');
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

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

组合继承最大的缺点是会调用两次父构造函数。

原型式继承

就是 ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。

function createObj(o) {   //传递一个字面量函数
 function F(){}     //创建一个构造函数
 F.prototype = o;    //把字面量函数赋值给构造函数的原型
 return new F()     //最终返回出实例化的构造函数
}
var person = {
    name: 'kevin',
    friends: ['daisy', 'kelly']
}

var person1 = createObj(person);
person1.friends.push('taylor');
console.log(person1.friends); // ["daisy", "kelly", "taylor"]


var person2 = createObj(person);
console.log(person2.friends); // ["daisy", "kelly", "taylor"]

缺点: 包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。

寄生式继承

创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。

function createObj(o) {
  let clone = Object.create(o);
  clone.sayName = function() {
    console.log('hi');  
  }
  return clone;
}

var person = {
    name: 'kevin',
    friends: ['daisy', 'kelly']
}

var person1 = createObj(person);
person1.friends.push('taylor');
console.log(person1.friends); // ["daisy", "kelly", "taylor"]


var person2 = createObj(person);
console.log(person2.friends); // ["daisy", "kelly", "taylor"]

缺点:

  1. 跟借用构造函数模式一样,每次创建对象都会创建一遍方法。
  2. 包含引用类型的属性值始终都会共享相应的值

寄生组合式继承

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);