在游戲引擎中使用繼承
(function(){
var initializing = false,
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
this.Class = function(){};
Class.extend = function(prop) {
var _super = this.prototype;
initializing = true;
var prototype = new this();
initializing = false;
for (var name in prop) {
prototype[name] = typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name]) ? (function (name, fn) {
return function() {
var tmp = this._super;
this._super = _super[name];
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) : prop[name];
}
function Class() {
if (!initializing && this.init) {
this.init.apply(this, arguments);
}
}
Class.prototype = prototype;
Class.prototype.constructor = Class;
Class.extend = arguments.callee;
return Class;
};
})();
這段代碼的主旨是允許使用extend方法從現(xiàn)有類中派生出新類尉间,繼承得來(lái)的對(duì)象可像父對(duì)象那樣共享相同的實(shí)例方法,可以通過(guò)子方法this._super()調(diào)用父方法扁瓢。這一特殊情況由代碼中部的循環(huán)進(jìn)行處理,該循環(huán)并非僅是一味的拷貝整個(gè)方法,它會(huì)檢查父類的現(xiàn)有方法鞠值,然后創(chuàng)建一個(gè)包裝器函數(shù),在調(diào)用期間渗钉,該函數(shù)臨時(shí)將this.super方法設(shè)置成父類的定義
for (var name in prop) { prototype[name] = typeof prop[name] == "function" && typeof _super[name] == "function" && fnTest.test(prop[name]) ? (function (name, fn) { return function() { var tmp = this._super; this._super = _super[name]; var ret = fn.apply(this, arguments); this._super = tmp; return ret; }; })
上述代碼檢查屬性是否已存在于超類中彤恶,若是,則創(chuàng)建一個(gè)函數(shù)鳄橘,在再次調(diào)用新方法之前声离,該函數(shù)會(huì)先對(duì)this._super執(zhí)行一個(gè)臨時(shí)賦值,若該方法不存在挥唠,代碼僅對(duì)屬性賦值抵恋,不會(huì)加入任何額外開(kāi)銷。
Class 代碼還加入了一個(gè)構(gòu)造函數(shù)宝磨,該函數(shù)自動(dòng)調(diào)用對(duì)象的init()方法弧关,還支持初始化函數(shù)的鏈?zhǔn)秸{(diào)用
function Class() { if (!initializing && this.init) this.init.apply(this, arguments); }
最后把extend方法添加到類中,這樣類就可以進(jìn)一步子類化:
Class.extend = arguments.callee;
調(diào)用arguments.calle時(shí),會(huì)返回被調(diào)用的方法(在該例中是extend),然后把方賦給所返回的Class對(duì)象的屬性extend,以此來(lái)支持進(jìn)一步的子類化娃殖。
運(yùn)用Class的功能
var Person = Class.extend({
init: function(){console.log('created person');},
speak: function(){console.log('person speaking');}
});
var p = new Person();
p.speak();
var Guy = Person.extend({
init: function(){this._super();console.log('created guy');},
speak: function(){this._super();console.log('i am a guy');}
});
var bob = new Guy();
bob.speak();
var Girl = Person.extend({
init: function(){console.log('created girl');},
speak: function(){console.log('i am a girl');}
});
var jill = new Girl();
jill.speak();
類功能使得以一種清晰的方式創(chuàng)建并擴(kuò)展類成為可能炼吴,這些子類確保超類的方法能在適當(dāng)?shù)臅r(shí)候被調(diào)用。
bob instanceof Person //true bob instanceof Guy //true bob instanceof Girl //false jill instanceof Person //true jill instanceof Guy //false jill instanceof Girl //true
該功能也簡(jiǎn)化了必要時(shí)對(duì)于對(duì)象類型的使用遵绰,可以注意到,bob和jill都如預(yù)期般響應(yīng)了instanceof運(yùn)算符。