What is the preferred way to export a prototype in node.js? You can take two approaches:
Export the prototype itself
function A () {
}
module.exports = A;
which is used as:
var A = require('./A.js');
var a = new A();
Export an object containing the prototype as property
function A () {
}
exports.A = A;
which is used as:
var A = require('./A.js').A;
var p = new A();
The first solution looks much more convenient to me, though I know there are concerns about replacing the exports object. Which of the two is best to use and why?
The second one would only be useful if you exported multiple classes from one file which is something that is questionable by itself.
There is no problem in replacing the exports object at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With