I have the following code that creates two objects (ProfileManager and EmployerManager) where the object EmployerManager is supposed to inherit from the object ProfileManager. However, when I do alert(pm instanceof ProfileManager); it returns false.
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);
BTW, this is my very first attempt at Object-Oriented JavaScript, so if you feel compelled to comment on the stupidity of my approach please feel free to do so, but offer suggestions on how to approach the problem better.
Thanks for all of the comments. However, the solution to the problem was found in making this change to the declaration of the EmployerManager:
Apparently JavaScript supports two different types of inheritance, function based and prototype based. The function based version is what I originally posted but could not make work because the EmployerManager could not see the loadData method that was part of ProfileManager’s prototype.
This new example is prototype based and works now. EmployerManager is now an instance of ProfileManager.