cocos2d-x for js 中集成了兩套繼承寫法,一套是 JR 的,一套是 google。公司同事使用過 node.js,對 google 的繼承方式比較贊同。我就看了一下 Google 的繼承代碼。 先貼代碼:
// 1) Google "subclasses" borrowed from closure library
// This is the recommended way to do it
//
cc.inherits = function (childCtor, parentCtor) {
/** @constructor */
function tempCtor() {};
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
childCtor.prototype.constructor = childCtor;
// Copy "static" method, but doesn't generate subclasses.
// for( var i in parentCtor ) {
// childCtor[ i ] = parentCtor[ i ];
// }
cc.inherits 是繼承函數(shù),負責(zé)鏈接父類和子類的原型鏈。非常有趣的是,在這里使用了一個臨時構(gòu)造器,這樣就替換了 JR 代碼中的 initializing 寫法??雌饋砗苁娣?。
cc.base = function(me, opt_methodName, var_args) {
var caller = arguments.callee.caller;
if (caller.superClass_) {
// This is a constructor. Call the superclass constructor.
ret = caller.superClass_.constructor.apply( me, Array.prototype.slice.call(arguments, 1));
return ret;
}
var args = Array.prototype.slice.call(arguments, 2);
var foundCaller = false;
for (var ctor = me.constructor;
ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
if (ctor.prototype[opt_methodName] === caller) {
foundCaller = true;
} else if (foundCaller) {
return ctor.prototype[opt_methodName].apply(me, args);
}
}
// If we did not find the caller in the prototype chain,
// then one of two things happened:
// 1) The caller is an instance method.
// 2) This method was not called by the right caller.
if (me[opt_methodName] === caller) {
return me.constructor.prototype[opt_methodName].apply(me, args);
} else {
throw Error(
'cc.base called from a method of one name ' +
'to a method of a different name');
}
};
cc.base 是在子類函數(shù)中調(diào)用父類同名函數(shù)的方法。要使用這個函數(shù),必須是使用過 cc.inherits 進行過鏈接原型鏈的類才行。參數(shù)方面,me 需要傳入 this,其他根據(jù)形參表來定。
var caller = arguments.callee.caller;
首先通過,上面的代碼獲得外層函數(shù)的對象。(據(jù)說 caller 這個屬性已經(jīng)不再建議使用了,不知道是什么原因)。 然后,如果外層函數(shù)是構(gòu)造函數(shù)的話,一定是存在 superClass_ 這個屬性的。那么可以用 apply 調(diào)用父類的構(gòu)造器,然后就退出函數(shù)執(zhí)行就可以了。(但是這里為什么會有返回值呢,他喵的構(gòu)造器返回值不是被運行環(huán)境給接管了么?)
var args = Array.prototype.slice.call(arguments, 2);
var foundCaller = false;
for (var ctor = me.constructor;
ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) {
if (ctor.prototype[opt_methodName] === caller) {
foundCaller = true;
} else if (foundCaller) {
return ctor.prototype[opt_methodName].apply(me, args);
}
}
如果外層函數(shù)不是構(gòu)造函數(shù),那么就是子類的普通函數(shù)。后面的代碼也很簡單,從子類向上往父類上面找,一層一層的遍歷構(gòu)造器,然后再核對同名函數(shù),如果在當前層次找到了對應(yīng)的函數(shù)名,就在下一輪循環(huán)中,調(diào)用父類的同名函數(shù)即可。然后直接返回。
if (me[opt_methodName] === caller) {
return me.constructor.prototype[opt_methodName].apply(me, args);
} else {
throw Error(
'cc.base called from a method of one name ' +
'to a method of a different name');
}
果要調(diào)用的那個函數(shù),既不是構(gòu)造函數(shù),也不是父類中的同名函數(shù)。那么只有一種可能,就是這個函數(shù)是子類的一個實例上的函數(shù)。直接 apply 調(diào)用就好了。
再找不到的話,代碼就會抽風(fēng)了。(throw Error)
綜上,google 的代碼風(fēng)格非常流暢,可讀性也很高。如果 JR 是很黃很暴力,各種奇技淫巧不計其數(shù)。那么 google 的代碼就是和風(fēng)細雨,潤物細無聲。
就我個人而已,非常喜歡 JR 的接口,但是又喜歡 google 的內(nèi)部實現(xiàn)。矛盾啊,喵了個咪。
另外,google 的代碼可以做到很容易的和其他繼承機制兼容,但 JR 的就不行,必須已自己為核心來搞才可以的。這些是由他們的實現(xiàn)機制決定的。
目前來說,cocos2d-x for js 使用 JR 的寫法,不知道會不會對將來的擴展造成一些問題呢。