function Box(_f){
this.f=_f;
}
// 这样直接将原型指向一个对象,会将原有的constructor覆盖
Box.prototype={
a:1,
b:function(){
this.a++;
}
};
// 因此这里我们使用定义属性,重新穿件这个constructor的指向
Object.defineProperty(Box.prototype,"constructor",{
writable:true,
value:Box
});
function extend(subClass,supClass){
function F(){}
F.prototype=supClass.prototype;
subClass.prototype=new F();
Object.defineProperty(subClass.prototype,"constructor",{
writable:true,
value:subClass
});
subClass.prototype.superClass=supClass.prototype;
if(supClass.prototype.constructor===Object) {
Object.defineProperty(supClass.prototype,"constructor",{
writable:true,
value:supClass
});
}
}
function Ball(_f){
// 在ES6等同于super(_f);
this.superClass.constructor.call(this,_f);
}
// ES6中 extends Box
extend(Ball,Box);
// Ball.prototype={
// //这样写会覆盖原有的原型,继承的东西都被覆盖了
// }
// 给原型上添加新的属性
Object.assign(Ball.prototype,{
c:10,
d:function(){
},
// 覆盖父类的方法
b:function(){
// ES6中super.b();
this.superClass.b.call(this);
this.a++;
console.log(this.a);
}
})
var c=new Ball(5);
console.log(c);
c.b();