You will need to make the constructor function an "instance" with a do
method as well. Assuming you want it to be a constructor (that constructs objects with a common prototype), the pattern might look like this:
function MyConstructor(prefix) {
this.pref = prefix ? prefix + " " : "";
}
MyConstructor.prototype.do = function(str) {
console.log(this.pref + str);
}
// now make it an "instance":
MyConstructor.call(MyConstructor, "");
utils.merge(MyConstructor.prototype, MyConstructor);
module.exports = MyConstructor;
Now you can call it either
var mod = new MyConstructor("some");
mod.do("stuff");
or
var mod = MyConstructor
mod.do("stuff");
You can make a similar thing for factories. Notice that if you want to insert if (!(this instanceof MyConstructor) return new MyConstructor(prefix);
to make it work without new
, instead of initialising the "instance" with MyConstructor.call(MyConstructor, "");
you will need to manually do MyConstructor.pref = "";