我有以下代碼創建兩個對象(ProfileManager和EmployerManager),其中對象EmployerManager應該從對象ProfileManager繼承。 但是,當我做alert(pm instanceof ProfileManager)時;它返回false。如何在JavaScript中建立對象之間的繼承關係?
function ProfileFactory(profileType) {
switch(profileType)
{
case 'employer':
return new EmployerManager();
break;
}
}
function ProfileManager() {
this.headerHTML = null;
this.contentHTML = null;
this.importantHTML = null;
this.controller = null;
this.actions = new Array();
this.anchors = new Array();
}
ProfileManager.prototype.loadData = function(action, dao_id, toggleBack) {
var step = this.actions.indexOf(action);
var prv_div = $('div_' + step - 1);
var nxt_div = $('div_' + step);
new Ajax.Request(this.controller, {
method: 'get',
parameters: {action : this.actions[step], dao_id : dao_id},
onSuccess: function(data) {
nxt_div.innerHTML = data.responseText;
if(step != 1 && !prv_div.empty()) {
prv_div.SlideUp();
}
nxt_div.SlideDown();
for(i = 1; i <= step; i++)
{
if($('step_anchor_' + i).innerHTML.empty())
{
$('step_anchor_' + i).innerHTML = this.anchors[i];
}
}
}
}
)
}
EmployerManager.prototype.superclass = ProfileManager;
function EmployerManager() {
this.superclass();
this.controller = 'eprofile.php';
this.anchors[1] = 'Industries';
this.anchors[2] = 'Employer Profiles';
this.anchors[3] = 'Employer Profile';
this.actions[1] = 'index';
this.actions[2] = 'employer_list';
this.actions[3] = 'employer_display';
}
var pm = new ProfileFactory('employer');
alert(pm instanceof ProfileManager);
順便說一句,這是我在面向對象的JavaScript非常的第一次嘗試,所以如果你覺得有必要對我的做法的愚蠢評論請隨時做,但就如何處理這個問題的建議更好。
的超從我的理解是父類。由於ProfileManager是EmployerManager的父代,因此ProfileManager是超類。 – 2008-11-21 19:51:51
這導致pm instanceof EmployerManager返回true,pm instanceof ProfileManager返回false。 – 2008-11-21 23:29:23