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.21
TRX 0.20
JST 0.033
BTC 94693.28
ETH 3119.47
USDT 1.00
SBD 3.05