Dav*_*EGP 7 javascript module node.js
最容易用代码解释:
##### module.js
var count, incCount, setCount, showCount;
count = 0; 
showCount = function() {
 return console.log(count);
};
incCount = function() {
  return count++;
};
setCount = function(c) {
  return count = c;
 };
exports.showCount = showCount;
exports.incCount = incCount;
exports.setCount = setCount; 
exports.count = count; // let's also export the count variable itself
#### test.js
var m;
m = require("./module.js");
m.setCount(10);
m.showCount(); // outputs 10
m.incCount();  
m.showCount(); // outputs 11
console.log(m.count); // outputs 0
导出的函数按预期工作.但我不清楚为什么m.count也不是11.
Ray*_*nos 14
exports.count = count
您将count对象上的属性设置为exports值count.即0.
一切都是通过价值而不是通过参考传递.
如果你要定义count为这样的getter:
Object.defineProperty(exports, "count", {
  get: function() { return count; }
});
然后exports.count总是返回当前值,count因此是11
| 归档时间: | 
 | 
| 查看次数: | 9005 次 | 
| 最近记录: |