How To Run Functions Referenced In A Javascript Dictionary

in #javascript5 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

Coin Marketplace

STEEM 0.23
TRX 0.25
JST 0.038
BTC 95511.18
ETH 3313.19
USDT 1.00
SBD 3.30