Javascript实现多继承

2010-11-11 16:43:56

转载请注明出处,感谢王政的指导,嘎嘎! 努力学习ing.....有问题欢迎留言讨论! [code] /** * Class创建器,支持多继承。只有第一父类采用 prototype继承,后续父类采用属性拷贝的方式。(可以参见cr&#101;ate函数) * * obj.initialize() 对象的构造器,对象创建时,会自己执行此方法 * * obj.className 对象的类名 * obj.superClasses 对象的父类定义(可以拥用多个父类) * obj.super() 对象的父类的引用 Creator.cr&#101;ate的第二个参数 * obj.instanceof(target) 判断对象是否为target的子类 * * @author kyomic * @mail:kyomic@163.com * version:1.1 * */ var Creator = function(){ if(arguments.callee == Creator){ throw new Error(&#34;静态类 Creator 不允许被构造.&#34;); } arguments.callee.cr&#101;ate = function(){ var arg = arguments; var superClasses = []; //重载对象数组 var overrideList = []; var thisClass = function(){ var super = thisClass.prototype.superClasses||[]; var i = 0; var len = super.length; for(i=0;i<len;i++){ if(typeof super[i]==&#34;function&#34;){ super[i].apply(this,arguments); if(super[i].prototype.initialize){ super[i].prototype.initialize.apply(this,arguments) } } } if (this.initialize) this.initialize.apply(this, arguments); this.pid = this.className+&#34;[&#34;+ Math.floor(Math.random()*10000)+&#34;]&#34;; //删除this中被重载的属性,将新的属性写入prototye var att,val; i = 0; len = overrideList.length; for(i=0;i<len;i++){ att = overrideList[i].att; val = overrideList[i].val; del&#101;te this[att]; arguments.callee.prototype[att] = val; } }; if(arg.length<1){ throw new Error(&#34;Creator 构造至少需要两个参数.&#34;); }else{ var cls = null; for(var i=1;i<arg.length;i++){ cls = arg[i]; if(typeof cls==&#34;function&#34;){ superClasses.push(cls); if(i>1){ //对于多个父类...怎么处理呢,进行属性拷贝吧。 for(var item in cls.prototype){ thisClass.prototype[item] = cls.prototype[item]; } }else{ var tmpF = new Function(); tmpF.prototype = new cls(); thisClass.prototype = new tmpF(); thisClass.prototype.constructor = thisClass; } } }; /** * * 注意,重载后,会删除thisClass中的动态创建的属性,将新的属性值写入prototype * @param att * @param fun */ thisClass.override = function(att,fun){ overrideList.push({&#34;att&#34;:att,&#34;val&#34;:fun}); }; thisClass.prototype.toString = function(){ return this.className; }; thisClass.prototype.super = function(){ var s = superClasses||[]; if(s &amp;&amp; s.length>0){ return new s[0](); } return this; }; thisClass.prototype.instanceof = function(parent){ var s = thisClass.prototype.superClasses||[]; for(var i=0;i<s.length;i++){ if(s[i]==parent)return true; if(s[i] &amp;&amp; s[i].instanceof){ if(s[i].instanceof(parent)) return true; } } return false; }; thisClass.prototype.superClasses = superClasses; thisClass.prototype.className = arg[0]; } return thisClass; } return arguments.callee; }(); /////测试代码/////////////// var GFather = function(name){ } var Father = function(name,value){ this.name = name; this.value = value; } Father.prototype.initialize = function(){ alert(1); } Father.prototype.getName = function(){ return &#34;AAAAAAAAAAAA&#34; } GFather.prototype.getValue = function(){ return &#34;VALUE.....&#34; } var cls = Creator.cr&#101;ate(&#34;MyClass&#34;,Father,GFather); cls.prototype.initialize = function(a,b,haha){ this.haha =haha } var obj = new cls(&#34;a&#34;,&#34;b&#34;,&#34;hahaha&#34;); Father.prototype.getName = function(){ return &#34;aaaaaaa&#34; } GFather.prototype.getValue = function(){ return &#34;value.....&#34; } alert.log(obj); //obj里会有 GFather和 Father的属性和方法 alert(obj.initialize) //返回 obj的构造器 alert(obj.super().initialize) //返回obj的第一父类(Father)的构造器 alert(obj.instanceof(GFather)); //返回true alert(obj.getValue()) //返回 value... (因为GFather属于拷贝式继承,返回不会因原型链的改变而被覆盖) alert(obj.getName()) //返回 AAAAAAAAAAAA ,Father为prototype继承方式,原型链被后继覆盖。 [/code] ----------修订,增加 Class.override方法,用于重写父类的方法(不推荐用子类的prototype来重写,父类通过this定义的方法,prototype没法重写,努力学习ing....)。 示例 var Father = function(){ this.fun = function(){ return:&#34;old&#34;; } } var Son = Creator.cr&#101;ate(&#34;Son&#34;,Father) Son.override(&#34;fun&#34;, function(){ return &#34;old is overrided&#34;; }) var obj = new Son(); obj.fun() //输出:old is overrided obj.super().fun();//输出:old