Javascript实现super()。
近日比较蛋疼,突然想起Java中的super很好用,心血来潮就用js模拟了下,欢迎拍砖。(注:由于super是js保留字,所有使用mysuper作为函数名。)
Object.prototype.mysuper = function(){
var caller = arguments.callee.caller,
name;
for(var i in this){
if(this[i] === caller){
name = i;
break;
}
}
__proto = this.__proto__ || this.constructor.prototype;
return __proto[name]();
}
function Class(){
this.name = "class";
this.setName = function(name){
this.name = name;
}
this.getName = function(){
return this.name;
}
}
function Test(){
this.getName = function(){
return 'sub-' + this.mysuper();
}
}
Test.prototype = new Class();
Test.prototype.constructor = Test;
var a = new Test();
alert(a.getName());
Fan.package('Fan.test'); // 创建一个包
// 创建类
Fan.clazz('Fan.test.A', function(){
var a = null; // 私有属性
this.aa = null; // 共有属性
// 构造方法
this.A = function(_a){
$super(); // 调用父类构造方法
a = _a;
};
this.init = function(){
alert2('A:init');
};
this.initEvent = function(){
alert2('A:initEvent');
this.show(1);
};
this.show = function(){
alert2('Fan.test.A');
};
});
// 创建类B,继承A
Fan.clazz('Fan.test.B', Fan.test.A, function(){
this.B = function(a, b){
$super(a); // 调用父类构造器,并传参数
};
this.init = function(){
alert2('B:init');
$super.init.call(this); // 调用父类方法
};
this._initEvent = function(){
alert2('B:initEvent');
$super.initEvent.call(this); // 调用父类方法
};
this.show = function(){
alert2('Fan.test.B');
};
});
// 创建C,继承B
Fan.clazz('Fan.test.C', Fan.test.B, function(){
// 当没有提供构造方法时,默认存在一个构造方法,且构造方法中只有一句代码$super();
//this.C = function(){
// $super();
//};
this.init = function(){
alert2('C:init');
this.initEvent();
};
this._initEvent = function(){
alert2('C:initEvent');
$super.getSuper().initEvent.call(this); // 调用父类的父类中的方法
};
this.show = function(p){
alert2('C:show ' + p);
if(p == 1){
$super.initEvent.call(this);
} else {
this.init();
}
};
});
Fan.clazz('Fan.test.D', Fan.test.C, function(){
this.D = function(){
$super();
};
this.init = function(){
alert2('D:init');
$super.init.call(this);
};
this._initEvent = function(){
alert2('D:initEvent');
$super.initEvent.call(this);
};
});
// 检测
a = new Fan.test.D();
a.show();
// 类型检测
a instanceof Fan.test.D // true
a instanceof Fan.test.C // true
a instanceof Fan.test.B // true
a instanceof Fan.test.A // true
a instanceof a.getClass() // true
a instanceof a.getClass().superClass // true
a instanceof a.getClass().superClass.superClass // true
a instanceof Object // true
在a的实例方法中 :$super instanceof this.getClass().superClass // true
Object.prototype.mysuper = function(){
var caller = arguments.callee.caller,
name;
for(var i in this){
if(this[i] === caller){
name = i;
break;
}
}
__proto = this.__proto__
[解决办法]
this.constructor.prototype;
try{
return __proto[name].apply(this, arguments);
}catch(e){
alert(name + ' is undefined.');
}
}
function Animal(){
this.name = "animal";
this.setName = function(name){
this.name = name;
}
this.getName = function(){
return this.name;
}
}
function Cat(){
this.name = "cat";
this.setName = function(name){
this.name = name;
}
this.getName = function(){
return this.name + this.mysuper();
}
}
function Test(){
this.getName = function(){
return 'sub-' + this.mysuper();
}
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
Test.prototype = new Cat();
Test.prototype.constructor = Test;
var a = new Test();
alert(a.getName());