大厂必面,js继承封装
Function.prototype.extends = function (superClass) {
var o = this.prototype;
function F() {}
F.prototype = superClass.prototype;
this.prototype=new F();
var names=Object.getOwnPropertyNames(o);
for(var i=0;i<names.length;i++){
var desc=Object.getOwnPropertyDescriptor(o,names[i]);
Object.defineProperty(this.prototype,names[i],desc);
}
this.prototype.superClass=superClass;
if(this.prototype.constructor!==this){
Object.defineProperty(this.prototype,"constructor",{
value:this
})
}
if(superClass.prototype.constructor!==superClass){
Object.defineProperty(superClass.prototype,"constructor",{
value:superClass
})
}
};
function CheckBox(){
}
CheckBox.prototype={
a:1,
b:function(){
}
}
function Radio(){
this.superClass.apply(this,arguments);
}
Radio.prototype={
c:10,
d:function(){
}
}
Radio.extends(CheckBox);
var r=new Radio();
console.log(r);