How To Run Functions Referenced In A Javascript Dictionary

in javascript •  6 years ago 

dict.jpg

Run functions from a dictionary



First we'll initialise a simple object/dictionary with some key/value pairs.

var dict = {
  text: 'Hello',
  action: 'fun'
}



Now on the the actual fun function. We'll declare it as a variable.

var fun = function(text) {
    alert(text);
}



Pull the key/value pairs from the dictionary and store them as variables.

var text = dict.text;
var action = dict.action;



Release the hounds.

window[action](text);



Thanks for reading. x

Resources

  • A keyboard
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!