小编Dav*_*EGP的帖子

节点模块 - 导出变量与导出引用它的函数?

最容易用代码解释:

##### 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
Run Code Online (Sandbox Code Playgroud)

导出的函数按预期工作.但我不清楚为什么m.count也不是11.

javascript module node.js

7
推荐指数
1
解决办法
9005
查看次数

标签 统计

javascript ×1

module ×1

node.js ×1