(function(){
function extend(func,proto){
func.prototype.__proto__=proto.prototype;
object.defineproperty(func.prototype,"proto",{
value: proto.prototype
});
}
function super(func,method){
if(!method) method='constructor';
return func.prototype.__proto__[method];
}
window.extend=extend;
window.super=super;
})();
在处理super的super时候遇到了死循环:
this.super-->this.proto.constructor(){this.super}-->this.proto.constructor。。。
后来直接用了上面代码中的办法,不想整得太复杂(就是不会。。。)
(function(){
function aaa(name){
this.name=name;
}
function bbb(name){
super(bbb).call(this,name);
}
extend(bbb,aaa);
function ccc(name,age){
super(ccc).call(this,name);
this.age=age;
}
extend(ccc,bbb);
var c=new ccc('ccc',18);
console.log(c);
})();
然后不想污染function,只能污染window了。。。
话说放在function里面是不是要好用些?
(function(){
function.prototype.extend=function(proto){
this.prototype.__proto__=proto.prototype;
}
function.prototype.super=function(method){
if(!method) method='constructor';
return this.prototype.__proto__[method];
}
})();
(function(){
function aaa(name){
this.name=name;
}
function bbb(name){
bbb.super().call(this,name);
}
bbb.extend(aaa);
function ccc(name,age){
ccc.super().call(this,name);
this.age=age;
}
ccc.extend(bbb);
var c=new ccc('ccc',18);
console.log(c);
})();