function Super(name){
this.name = name || 'Tom'
this.colors = [1, 2, 3, 4]
}
Super.prototype.getName = function() {
return this.name
}
// 原型继承
// 缺点:实例中的属性会被共享 无法向超类传递参数
// function Sub() {
// this.age = 26
// }
// Sub.prototype = new Super()
// Sub.prototype.getAge = function() {
// return this.age
// }
// Sub.prototype.constructor = Sub
// const instance1 = new Sub()
// instance1.colors.push(5)
// console.log(instance1.colors)
// const instance2 = new Sub()
// console.log(instance2.colors)
// 利用构造函数
// 缺点:超类定义的方法对外不可见,函数复用无从弹起
// function Sub(...args) {
// Super.apply(this, args)
// }
// const instance1 = new Sub()
// instance1.colors.push(5)
// console.log(instance1)
// console.log(instance1.colors)
// console.log(instance1.getName())
// const instance2 = new Sub()
// console.log(instance2.colors)
// 组合继承
// 缺点:超类会被调用两次 原型链上多余的超类属性
// function Sub(...args) {
// this.age = 26
// Super.apply(this, args)
// }
// Sub.prototype = new Super()
// Sub.prototype.getAge = function() {
// return this.age
// }
// Sub.prototype.constructor = Sub
// const instance1 = new Sub('Marry')
// instance1.colors.push(5)
// console.log(instance1)
// console.log(instance1.colors)
// console.log(instance1.getName())
// const instance2 = new Sub()
// console.log(instance2.colors)
// 寄生组合继承
// function clonePrototype(Super, Sub) {
// const prototype = Object.create(Super.prototype)
// prototype.constructor = Sub
// Sub.prototype = prototype
// }
// function Sub(...args) {
// this.age = 26
// Super.apply(this, args)
// }
// clonePrototype(Super, Sub)
// Sub.prototype.getAge = function() {
// return this.age
// }
// const instance1 = new Sub('Marry')
// console.log(instance1)
// instance1.colors.push(5)
// console.log(instance1.colors)
// console.log(instance1.getName())
// const instance2 = new Sub()
// console.log(instance2.colors)
console