低耦合,高内聚
重载&重写
其实这是后台的知识,这么做的原因是:涉及到服务器的承压能力。减轻并发数
子类重写父类中的方法
一切皆对象……学院派的答法,尽量不要。
手动加上constructor,保证原型重定向的完整性
原型继承:
改变原型链的指向, 让子类的原型链上出现父类的原型,让子类的原型指向父类的某个实例。
3.我们希望的是,私有的变为私有的,公有的变为公有的
function A(x){this.x=x;
}
A.prototype.getX=function(){
console.log(this.x);
}
// 原型继承
B.prototype=new A(100)
B.prototype.constructor=B;
function B(y){this.y=y;
}
B.prototype.getY=function(){
console.log(this.y);
}
子类继承了父类的私有属性,也继承到了子类的私有属性上
es6之前,相对来说比较完整的方式
扩展一下 Object.create
Object.create(proto) 创建一个空对象,让其__proto__指向proto
function A(x){// console.dir(this);this.x=x;
}
A.prototype.getX=function(){
console.log(this.x);
}
B.prototype=Object.create(A.prototype);
B.prototype.constructor=B;
function B(x,y){A.call(this,x)this.y=y;
}
B.prototype.getY=function(){
console.log(this.y);
}
let b=new B(100,200)
console.dir(b);
// console.dir(Object.create(A.prototype))