【cocos2d-x从c++到js】06:Google的继承写法解析
cocos2d-x for js中集成了两套继承写法,一套是JR的,一套是google。公司同事使用过node.js,对google的继承方式比较赞同。我就看了一下Google的继承代码。
先贴代码:
//1)Google"subclasses"borrowedfromclosurelibrary//Thisistherecommendedwaytodoit//cc.inherits=function(childCtor,parentCtor){/**@constructor*/functiontempCtor(){};tempCtor.prototype=parentCtor.prototype;childCtor.superClass_=parentCtor.prototype;childCtor.prototype=newtempCtor();childCtor.prototype.constructor=childCtor;//Copy"static"method,butdoesn'tgeneratesubclasses.//for(variinparentCtor){//childCtor[i]=parentCtor[i];//}};
cc.inherits是继承函数,负责链接父类和子类的原型链。非常有趣的是,在这里使用了一个临时构造器,这样就替换了JR代码中的initializing 写法。看起来很舒服。
cc.base=function(me,opt_methodName,var_args){varcaller=arguments.callee.caller;if(caller.superClass_){//Thisisaconstructor.Callthesuperclassconstructor.ret=caller.superClass_.constructor.apply(me,Array.prototype.slice.call(arguments,1));returnret;}varargs=Array.prototype.slice.call(arguments,2);varfoundCaller=false;for(varctor=me.constructor;ctor;ctor=ctor.superClass_&&ctor.superClass_.constructor){if(ctor.prototype[opt_methodName]===caller){foundCaller=true;}elseif(foundCaller){returnctor.prototype[opt_methodName].apply(me,args);}}//Ifwedidnotfindthecallerintheprototypechain,//thenoneoftwothingshappened://1)Thecallerisaninstancemethod.//2)Thismethodwasnotcalledbytherightcaller.if(me[opt_methodName]===caller){returnme.constructor.prototype[opt_methodName].apply(me,args);}else{throwError('cc.basecalledfromamethodofonename'+'toamethodofadifferentname');}};
cc.base是在子类函数中调用父类同名函数的方法。要使用这个函数,必须是使用过cc.inherits进行过链接原型链的类才行。参数方面,me需要传入this,其他根据形参表来定。
varcaller=arguments.callee.caller;
首先通过,上面的代码获得外层函数的对象。(据说caller这个属性已经不再建议使用了,不知道是什么原因)。
然后,如果外层函数是构造函数的话,一定是存在superClass_这个属性的。那么可以用apply调用父类的构造器,然后就退出函数执行就可以了。(但是这里为什么会有返回值呢,他喵的构造器返回值不是被运行环境给接管了么?)
varargs=Array.prototype.slice.call(arguments,2);varfoundCaller=false;for(varctor=me.constructor;ctor;ctor=ctor.superClass_&&ctor.superClass_.constructor){if(ctor.prototype[opt_methodName]===caller){foundCaller=true;}elseif(foundCaller){returnctor.prototype[opt_methodName].apply(me,args);}}
如果外层函数不是构造函数,那么就是子类的普通函数。后面的代码也很简单,从子类向上往父类上面找,一层一层的遍历构造器,然后再核对同名函数,如果在当前层次找到了对应的函数名,就在下一轮循环中,调用父类的同名函数即可。然后直接返回。
if(me[opt_methodName]===caller){returnme.constructor.prototype[opt_methodName].apply(me,args);}else{throwError('cc.basecalledfromamethodofonename'+'toamethodofadifferentname');}
如果要调用的那个函数,既不是构造函数,也不是父类中的同名函数。那么只有一种可能,就是这个函数是子类的一个实例上的函数。直接apply调用就好了。
再找不到的话,代码就会抽风了。(throw Error)
综上,google的代码风格非常流畅,可读性也很高。如果JR是很黄很暴力,各种奇技淫巧不计其数。那么google的代码就是和风细雨,润物细无声。
就我个人而已,非常喜欢JR的接口,但是又喜欢google的内部实现。矛盾啊,喵了个咪。
另外,google的代码可以做到很容易的和其他继承机制兼容,但JR的就不行,必须已自己为核心来搞才可以的。这些是由他们的实现机制决定的。
目前来说,cocos2d-x for js使用JR的写法,不知道会不会对将来的扩展造成一些问题呢。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。