B继承A的写法

来源:互联网 发布:mini mac恢复出厂设置 编辑:程序博客网 时间:2024/05/16 04:59

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();




原创粉丝点击