I am trying to do something kind of polymorphic object oriented in Javascript where I have a map of object 'types' and each one has its own properties and functions which I can call.
While the properties work fine the functions do not and I cant understand why. In Firefox I get the error: TypeError: this.functionmap[type].action is not a function
Here is my code:
var object = {
flapWings : function(count) { alert("I am flapping my wings "+count+" times"); },
kick : function(count) { alert("I am kicking my legs "+count+" times"); },
functionmap : {
"bird" : { label : "Bird", action : this.flapWings },
"horse" : { label : "Horse", action : this.kick }
},
doAction : function (type, count) {
alert("I am a "+this.functionmap[type].label);
this.functionmap[type].action(count);
}
};
object.doAction("horse", 5);
Here is JSFiddle example:
I just dont see why: action : this.kick is not getting a reference to the kick function created right above it!
I want to avoid something stupid like action : function(count) : this.kick(count); even though that doesnt work either - I want a direct reference without having to retype the parms