I’m able to store a functions in an object literal, as such:
var GameEvents = {
player_position_update: function (data) {
// work on data
},
player_connect: function (data) {
// work on data
}
}
So I want to call the function by its name in a variable:
var event_name = "player_position_update";
GameEvents[? event_name](data)
I’ve tried everything I can think of to call a function inside an object literal with a variable key.
I’ve tried both GameEvents[@ event_name](data)
, and GameEvents[? event_name](data)
, and GameEvents[event_name](data)
and I’m still not able to. I can’t find this specific question anywhere in my searches.
New contributor
Metaphoresy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.