近日比较蛋疼,突然想起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());
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());
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 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());
想法不错。js实现面向对象编程模式,其中就需要定义关键字super。给你看一段代码:实现了super关键字 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
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());lz你看下上面这段代码。
如果单单只是实现一个父类,一个子类这样的。用this当然没什么问题。但是如果是父类,子类,进一步子类这样的继承链。用this就完蛋了。因为this只会指向最低端的类(因为会有对象冒充)。这也是为什么很多开源框架都用类似于静态变量的方法来实现super的根本原因。另外,你的方法动了object的原型,不动原型的方法我的博客里有:
http://rainsilence.iteye.com/blog/1026530
例子只是片面的,只要你想用,完全可以:$super.initEvent()。给你看的只是个例子。当你没有动手去实现一个super的时候,你不懂js与java的super的区别。我的$super,无需定义,就像arguments,进入方法中,即可使用。因为它是个关键字,而且是个仅在方法中才可访问到的特殊关键字。它既是父类的构造方法,又是父类的实例对象。可以使用instanceof鉴别。你知道java中new一个子类,其内存中也同时构造了父类对象么?在深层次继承中,它还可以访问任意一层次的祖类对象,这点java并没提供。例子随简单,至少你看懂了,对吧。本意就是让会java的人,能直接看懂js写的代码。$super.initEvent.call(this);这句代码并不丑,你只是不喜欢这尾巴“call(this)”,js模拟多态,肯定要用call或apply函数。即使不明写,那也将会隐式用到call。如何隐式?var proxy = function(fn, me){
return function(){
return fn.apply(me, arguments);
};
};
在构造父类时,将所有方法都处理一次:for (var m in $super) {
$super[m] instanceof Function && ($super[m] = proxy($super[m]));
}
生产之后的$super再调用initEvent,只需$super.initEvent(); 但是我并没这么做,why? 因为我觉得加个call(this)并不难看,而且,更容易看懂。凡事没有必要过度封装。
for (var m in $super) {
$super[m] instanceof Function && ($super[m] = proxy($super[m],$super));
}
漏了个$super没传入另:实现super时,千万别让他和this有任何关联。不然,你会遇到无法处理多态中细节部分的尴尬。
还有,如果用到call,大可不必动用Object原型,定义一个全局函数super即可。
动用Object原型?貌似是你的代码动了原型,$super是我自定义的关键字。它是私有的。定义全局$super?首先造成污染,而且super是每个类独有的,不能单列。原因是你不知道我何时调用方法更没办法在我调用任意方法时,立即将super指到正确的对象上。更更没办法知道我是在嵌套调用,如:this.show(),show里面又有super.show(),里面又有this.hide(),hide中又用到super....这些super调用会陷入嵌套调用里面,而却每个方法调用完毕后,又要归还super的指向。你考虑过没?全局能做到?绑定到this上能做到?都不能。所以需要自定义个$super关键字,它跟类走,它永远指向当前类的父类。这么说吧,你在java中如何使用super,那么这个$super就一样的用法。它的作用域不会超出方法之外。这是我自己写的一个框架,名字叫Fan,扇子的意思。其中OOP模块,全是参照java。包括super的概念和用法。在IE里super是保留关键字,不可用。才叫$super。类的外部是访问不到的。看了15楼仁兄的实现方式, 不用function做继承,将丧失很多东西,1、失去instanceof派生对象类型鉴别2、失去var创建私有属性,失去封装性3、丧失new关键字构建对象4、缺少super的概念,直接拿父类名称Cat.xxx来使用这样的OOP模式,说实话,我不喜欢。