[英]Exporting variables and functions in nodejs
我正在嘗試將一組全局變量和函數從一個Javascript文件導出到nodejs中的另一個。
來自node-js.include.js
var GLOBAL_VARIABLE = 10;
exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports = {
add: function(a, b) {
return a + b;
}
};
進入test-node-js-include.js :
var includes = require('./node-js-include');
process.stdout.write("We have imported a global variable with value " + includes.GLOBAL_VARIABLE);
process.stdout.write("\n and added a constant value to it " + includes.add(includes.GLOBAL_VARIABLE, 10));
但變量; 我得到以下輸出:
We have imported a global variable with value undefined
and added a constant value to it NaN
為什么不出口GLOBAL_VARIABLE ?
2種解決方法:
module.exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports.add: function(a, b) {
return a + b;
};
module.exports.GLOBAL_VARIABLE = GLOBAL_VARIABLE;
module.exports = {
add: function(a, b) {
return a + b;
},
GLOBAL_VARIABLE: GLOBAL_VARIABLE
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.