js实现继承

1.借用构造函数继承(call继承),私对私:
把父类当做普通函数执行,让其执行的时候,方法中的this变为子类的实例即可
缺点:只能继承 A 中的私有变量,不能继承原型上的方法
function A(){
    this.x = 'x';
}

A.prototype.getX = function(){
    console.log(this.x)
}
function B(){
    A.call(this);
    this.y = 'y'
    this.getY = function(){
        console.log(this.y)
    }
}
let b = new B();
console.log(b)
2.原型继承:
公对公:将子类的原型指向父类的原型,但并没有继承其私有属性
缺点:并没有将父类的私有属性变成子类的私有属性。
function A(){
   this.x = '1';
}
A.prototype.getX = function(){
   console.log(this.x)
}
function B(){
   this.y = 'y'
}
B.prototype = new A();//子类的原型指向父类的实例,因此子类也能访问到父类的私有属性,以及父类的原型方法
B.prototype.constructor = B;//原型上的构造函数应该也是它自己
B.prototype.getY = function getY() {
   console.log(this.y);
};
let b = new B();
console.log(b);
3.组合继承
call继承+原型继承的组合
缺点:调用了两次父类的构造函数,并将父类的私有属性在子类里,和子类的原型里都放了一份。
function A(){
   this.x = '1';
}
A.prototype.getX = function(){
   console.log(this.x)
}
function B(){
   A.call(this);
   this.y = 'y'
}
B.prototype = new A();
B.prototype.getY = function getY() {
   console.log(this.y);
};
let b = new B();
console.log(b);
4.寄生组合继承:相对于组合继承来说,不会再调用两次父类的构造函数
function superType(name){
   this.name = name;
   this.colors = ['red','blue'];
}
superType.prototype.jump = function(){
   console.log('jump');
}

function subType(){
   superType.call(this,'李锦涛')
    this.age = 20
}

//通过创建一个新对象,并将新对象的原型指向父类的原型,再将子类的原型指向此对象,这样只继承了父类原型,并没有将父类的私有属性也添加到原型上
var obj = Object.create(superType.prototype);
obj.constructor = subType.constructor;
subType.prototype = obj;

subType.prototype.run = function(){
   console.log('run');
}

let sub = new subType();
console.log(sub);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值