es5 继承
原型继承缺点就是new 之后的实例对象指向同一个构造函数的原型对象,构造函数继承通过call,aplly的缺点就是无法继承构造函数原型上的属性与方法。组合继承(原型继承、构造函数继承)解决这个问题。 ```javascript
// 寄生组合(原型、构造函数)继承function Parent() {this.name = 'parent'this.play = [1, 2, 3]}Parent.prototype.getName = function () {console.log(this.name)}function Child() {Parent.call(this)this.friends = 'child'}
// 寄生继承(浅拷贝)
function clone(parent, child) {child.prototype = Object.create(parent.prototype) // 或者child.prototype= new Parent()child.prototype.constructor = child}clone(Parent, Child)Child.prototype.sayName = function () {console.log(this.friends)}let person = new Child()person.sayName()person.getName()
<a name="HHmPK"></a>### es6 类继承```javascript// es6继承class Parent {constructor() {this.name = 'parent'this.play = [1, 2, 3]}getName = function () {console.log(this.name)}}class Child extends Parent {constructor() {super()this.friends = 'child'}sayName = function () {console.log(this.friends)}}let person = new Child()person.sayName()person.getName()
