继承

102 阅读1分钟

一、何为继承

一个类获取另一个或者多个类的属性或者方法。
继承可以使得子类具有父类的各种方法和属性。以免重复输出很多代码。

二、继承的原理

复制父类的方法和属性来重写子类的原型对象。

三、原型链继承

//最简单实现

function Parent() {
    this.text = 'text1';
}
Parent.prototype.someFn = function() {
    console.log(11111);
}
Parent.prototype.someValue = 'text2';

function Child(){
    this.text1 = 'text1';
}
// 函数原型指向构造函数的实例
Child.prototype = new Parent();