es5继承类型:
- 原型链继承:让构造函数的原型指向父类的实例,缺点是子类没有办法构建私有属性,对一个子类实例的属性进行更改,会改变所有子类的实例
function Parent(){
this.name = ['张三']
}
Parent.prototype.getName = function(){
return this.name
}
function Child(){
}
Child.prototype = new Parent()
Child.prototype.constructor = Child
let c1 = new Child()
let c2 = new Child()
console.log(c1.getName()) // ['张三']
console.log(c2.getName()) // ['张三']
c1.name.push('李四')
console.log(c2.getName()) // ['张三', '李四']
- 借用构造函数继承:利用call或apply,将父类的构造函数的this变为子类,缺点是无法获取到父类原型对象上的方法或属性
function Parent(name)