B.prototype=new A;// {getX:function(){}}
var a = new A;//
var b = new B;
//a.haowan();
b.haowan();
b.getX();
原型继承的案例
function A() {
this.getX=function () {
console.log(100);
}
function getY() {
}
}
A.prototype.haowan=function () {
console.log("天下雨了快")
}
function B() {
}
B.prototype=new A;// {getX:function(){}},new A把B的原型重新赋值
//var a = new A;//
var b = new B;
//a.haowan();
b.haowan();
b.getX();
function A() {
this.haowan=100;
}
A.prototype.getY = function () {
}
function B() {
//this-->b;也就是B的实例
this.a=200;
A.call(this);//改变A中的this,指向类B的实例;让A函数运行
}
var a = new A;
var b = new B;
console.log(b.haowan);//100
寄生组合继承(继承公有和私有)
function A() {
this.x=100;
}
A.prototype.getX =function () {
}
function B() {
A.call(this)
}
// 让B的原型中的__proto__指向A的原型;那么通过类B创建的实例;既可以使用A的私有属性,也可以使用公有属性;
//B.prototype=A.prototype;
B.prototype = Object.create(A.prototype);// {__proto__:A.prototype}
B.prototype.getY = function () {
}
var b = new B;
console.log(b.constructor);// A