對於稍微成型的想法抱有歉意,但是在開始使用Node.js時,我確定我在某處讀到,因爲模塊是對象,this.myFunc
對模塊內exports.myFunc
等公共訪問的屬性同樣適用,如下所示:在Node.js模塊中使用「exports」和「this」有什麼區別?
app.js
var test = require('./modtest');
console.log(test.foo());
console.log(test.bar());
modtest.js
this.foo = function() { return "abc" }
exports.bar = function() { return "xyz" }
除了做「正確的事情」,爲什麼我應該使用exports
對象?