Access to variable regions of a NodeJS node module as an object

I can access the global variables node as an object GLOBAL.

Can I access the scope variables of a module in the same way?

eg.

var fns_x = function(){/*...*/};
var fns_y = function(){/*...*/};

function aFn(param){
   /* moduleScope = something that allows me to access module scope variables */
   if(moduleScope['fns_' + param]){
      moduleScope['fns_' + param]();
   }
}

/*...*/
module.exports = /*...*/

Or is it better to wrap these variables in an object? eg.

var fns = {
   x: x = function(){/*...*/},
   y: x = function(){/*...*/}
}

function aFn(param){
   if(fns[param]){
      fns[param]();
   }
}

/*...*/
module.exports = /*...*/
+4
source share
1 answer

The short answer is NO.

Although the ECMAScript specifications are available in the specification specification in the configuration environment setting for the module, it is indicated that this should not be available programmatically.

ECMAScript cannot directly access or manipulate these values [spec]

.

var registry = {
   x: function () {},
   y: function () {},
};


module.exports = function (prop) { 
  return registry[prop]; 
};

, .

, , .

var that = this;

that.x = function () {};
that.y = function () {};

 module.exports = function (prop) { 
    return that[prop]; 
 };
+1

All Articles