我是靠谱客的博主 深情草丛,这篇文章主要介绍B继承A的写法,现在分享给大家,希望可以做个参考。

1.原型继承:

var A=function(){
this.a=1;
this.b=2;
this.add=function(){
console.log(a+b);
}
}
var B=function(){}
B.prototype=new A();
B.prototype.c=3;
B.prototype.add=function(){
console.log(B.a+B.b+B.c);
}
var b=new B();
console.log(b.a);
console.log(b.b);
console.log(b.c);
b.add();
2、构造函数继承

var A=function(){
this.a=1;
this.b=2;
this.add=function(){
console.log(a+b);
}
}
var B=new A();
B.c=3;
B.add=function () {
console.log(B.a+B.b+B.c);
}
console.log(B.a);
console.log(B.b);
console.log(B.c);
B.add();
3、call、apply实现继承
function A(){
this.a=1;
this.b=2;
this.add=function(){
console.log(a+b);
}
}
function B(){
A.call(this);
this.c=3;
this.add=function(){
console.log(this.a+this.b+this.c);
}
}
var b=new B();
console.log(b.a);
console.log(b.b);
console.log(b.c);
b.add();




最后

以上就是深情草丛最近收集整理的关于B继承A的写法的全部内容,更多相关B继承A内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(144)

评论列表共有 0 条评论

立即
投稿
返回
顶部