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.26
TRX 0.13
JST 0.031
BTC 61699.33
ETH 2887.31
USDT 1.00
SBD 3.47