I'm programming two pieces of a software.
var SDK = require("./sdk");
SDK("key", "secret");
var M1 = require("./sdk/m1");
var m1 = new M1();
m1.doSomething();
var M2 = require("./sdk/m2");
var m2 = new M2();
m2.doSomethikng()
var SDK = function(key, secret) {
this.key = key;
this.secret = secret;
}
module.exports = SDK;
var M1 = function() {}
M1.prototype.doSomething = function() {
//uses key and secret
}
module.exports = M1;
var M2 = function() {}
M2.prototype.doSomething = function() {
//uses key and secret
}
module.exports = M2;
Here's a pattern that I've discovered without Factory methods on the main module (can't have factory method for each of my several modules) while the other actors still have access to config data which is held by main module only.
app.js
var SDK = require("./sdk");
SDK.config = {key: "key", secret: "secret"};
var m1 = new SDK.M1();
m1.doSomething();
sdk.js
var SDK = function(){};
SDK.config = {key: null, secret: null};
module.exports = SDK;
require("./m1"); //executing sub modules to make it work
m1.js
var SDK = require("./sdk");
var M1 = function(){};
M1.prototype.doSomething = function(){
var config = SDK.config //getting access to main module's config data
};
SDK.M1 = M1;
And same with the other actors of the system, there's no passing of config parameters around and forcing each actor to have a similar constructor.