NodeJS: Passing a variable into a different variable's name

How would I create a function that accepts a value and creates a watchfile listener based on that value received?

Example of what I'm trying to do:

function createListener(id) {
  var listener(id here) = fs.watchFile(file, function () {
  });
});

How could I place the id given to function createListener(); into the listener's assigned variable?

Example: createListener('5'); would create var listener5 = fs.watchFile();

createListener('23'); would create var listener23 = fs.watchFile();

You can use eval to create dynamic variable names

but it is bad part of JavaScript and I strongly recommend not to use eval at all.


I recommend you to use either array or JSON objects to store multiple functions.

Array

var functions = [];
functions.push = function () { ... };

// functions = [ function () { ... } ];

JSON

var functions = {};
functions["one"] = function () { ... };

// functions = { one: function () { ... } };