I've a JavaScript class, and I'd like to override a parent method by creating a child class. However, I am struggling to work out how to call the child method from the context of the parent.
Here's a trimmed-down version of my parent:
// "rules" is a global hash
function ForumFilter() {
this.scanText = function(title, body) {
// Save 'this' context, as each() overwrites it
var that = this;
// This is jQuery each()
$.each(rules, function(ruleName, rule) {
// rule.search is a regex
var match = rule.search.test(body);
if (match)
{
that.isPassed = false;
// ** I'd like to call a child method here,
// ** but it only calls the method in this class
that.setRuleFailed(ruleName);
}
});
}
this.setRuleFailed = function(ruleName) {
this.failedRules.push(ruleName);
}
}
Here's my attempt at the child:
ForumFilterTest.prototype = new ForumFilter();
ForumFilterTest.prototype.setRuleFailed = function(ruleName) {
// Call parent
ForumFilter.setRuleFailed(ruleName);
// Record that this one has triggered
this.triggered.push(ruleName);
}
Here's my calling my parent method from a child instance:
var scanner = new ForumFilterTest();
scanner.scanText("Hello", "Hello");
So, in scanText (which only exists in the parent) it may call setRuleFailed, which should call the version in ForumFilterTest, which in turn calls the class it overrides. Thus, as its name implies, I am trying to add a behaviour to the parent for testing purposes, so of course I want the parent method to be used if ForumFilter is instantiated on its own.