Given e.g. a class instance of some sort has a state (e.g. \'active\', \'inactive\', …). The instance also attaches a click event to e.g. a link but the event handler does somet
I would keep the same handler and call the appropriate method within.
var Foo = (function(){
function Foo() {
this.state = 'active';
}
Foo.methodMapping = {
active: 'a',
inactive: 'b'
};
Foo.prototype = {
a: function(){}.
b: function(){},
handler: function(el) {
// This'll handle the event, I guess
// (Assuming `this` refers to instance, not element)
var state = this.state;
if (state in Foo.methodMapping) {
return this[Foo.methodMapping[state]].apply(this, arguments);
} else {
// (prob don't need to cover this case)
}
}
};
return Foo;
}());