I've been trying to get to grips with OO JavaScript and created a simple example.
function BasePage(name) {
this.init(name);
}
BasePage.prototype = {
init: function(name) {
this.name = name;
},
getName: function() {
return this.name;
}
}
function FaqPage (name, faq) {
this.init(name, faq);
}
FaqPage.prototype = new BasePage();
FaqPage.prototype = {
init: function(name, faq) {
BasePage.prototype.init.call(this, name);
this.faq = faq;
},
getFaq: function() {
return this.faq;
}
}
var faqPage = new FaqPage('Faq Page', 'Faq');
var text = faqPage.getName() + ' ' + faqPage.getFaq();
$('body').text(text);
The result of running this results in the following message:
Uncaught TypeError: Object
#<Object>
has no method 'getName'
What I would like to know is how can I call the method getName()
in the super class without having to override and call it in the sub class?
Also if my if you think this approach is bad/good.