In this tutorial the author uses this code to exemplify exporting code:
// sum.js
var sum = function (a, b) {
return a + b;
};
module.exports = sum;
But isn't this wrong? I thought the module.exports
is an object and you're suppose to assign the key of sum
to it and the value
of the function to it similar to something like what this other article did -
// greetings.js
// var exports = module.exports = {};
exports.sayHelloInEnglish = function() {
return "HELLO";
};
exports.sayHelloInSpanish = function() {
return "Hola";
};
. Did the first author mess up?
module.exports
is just a reference of what will be returned from require()
. You can set its value to an object
containing methods as well as you can just set it's value to a function or any other assignable value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.