// -------- node.js core --------
var module = {
exports: {
}
};
exports = module.exports;
// -------- 下面正常寫代碼 --------
exports.name = 'Alan';
exports.test = function () {
console.log('hi')
};
// 給導(dǎo)出的對象添加屬性顽聂,直接這樣就好了
console.log(module) // { exports: { name: 'Alan', test: [Function] } }
exports = {
name: 'Bob',
add: function (a, b) {
return a + b;
}
}
// 不過 ① exports 是一個引用绿聘,直接賦值給它精偿,只是讓這個變量等于另外一個引用
console.log(exports) // { name: 'Bob', add: [Function] }
// 并不會修改到導(dǎo)出的對象
console.log(module) // { exports: { name: 'Alan', test: [Function] } }
module.exports = {
name: 'Bob',
add: function (a, b) {
return a + b;
}
}
// ∵① 所以 只有通過 module.exports 才能真正修改到 exports 本身
console.log(module) // { exports: { name: 'Bob', add: [Function] } }
轉(zhuǎn)自:https://cnodejs.org/topic/5734017ac3e4ef7657ab1215