JavaScript继承本质和原型链有关
- 原型、实例、构造函数、原型链之间的关系

# 一、借助构造函数实现继承
- 缺点:实例无法访问父类原型上的属性
function Parent1 () {
this.name = 'parent1';
}
Parent1.prototype.say = function () {
};
function Child1 () {
Parent1.call(this);
this.type = 'child1';
}
// Parent1原型上的say没法拿到
console.log(new Child1(), new Child1().say());
@前端进阶之旅: 代码已经复制到剪贴板
# 二、借助原型链实现继承
- 缺点:子类和父类公用一个原型,最后导致子类的多个实例都是一样的,无法隔离
function Parent2 () {
this.name = 'parent2';
this.play = [1, 2, 3];
}
function Child2 () {
this.type = 'child2';
}
Child2.prototype = new Parent2();
var s1 = new Child2();
var s2 = new Child2();
s1.play.push(4);
console.log(s1.play, s2.play); // [1, 2, 3, 4] [1, 2, 3,4] 结果是一样的,说明两个实例无法隔离
@前端进阶之旅: 代码已经复制到剪贴板
# 三、组合继承方式
- 此方法解决了上面的实例相同的问题,但是父类的构造函数执行了多次
function 