I'm wondering if there are any pros or cons when using the two approaches against each other:
first.js:
this.myFunction = function() {
return 'herro first';
}
second.js:
module.exports = obj = {};
obj.myFunction = function() {
return 'herro second';
}
The two above would then be included and used as so:
app.js:
var first = require('./first.js');
console.log(first.myFunction());
var second = require('./second');
console.log(second.myFunction());
module.exports
(or just exports
) is the standard CommonJS way.
In Node.js, this
happens to be the same object, but that is best not relied on, and using this
will not work with other tools, for example Browserify
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