Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js:需要局部变量 我想要什么_Node.js_Module_Require_Locals - Fatal编程技术网

Node.js:需要局部变量 我想要什么

Node.js:需要局部变量 我想要什么,node.js,module,require,locals,Node.js,Module,Require,Locals,是否可以将局部变量传递到所需的模块? 例如: // in main.js var words = { a: 'hello', b:'world'}; require('module.js', words); // in module.js console.log(words.a + ' ' + words.b) // --> Hello World 我这样问是因为在PHP中,当您需要或包含其他文件时,包含其他文件的文件会继承其变量,这在某些情况下非常有用,如果这也可以在node.js中

是否可以将局部变量传递到所需的模块? 例如:

// in main.js
var words = { a: 'hello', b:'world'};
require('module.js', words);

// in module.js
console.log(words.a + ' ' + words.b) // --> Hello World
我这样问是因为在PHP中,当您需要或包含其他文件时,包含其他文件的文件会继承其变量,这在某些情况下非常有用,如果这也可以在node.js中完成,我会很高兴

我尝试过但没有成功的东西
这两个函数都给出了
ReferenceError:在
module.js中调用
words
时未定义单词

module.exports = function(words){
    console.log(words.a + ' ' + words.b);
};
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
var obj = require(mymod)(5,6);
module.exports = function(a,b){
 return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};

那么,如果没有全局变量,是否有可能呢?

您要做的是使用一个参数导出它,以便可以将变量传递给它

module.js

module.exports = function(words){
    console.log(words.a + ' ' + words.b);
};
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
var obj = require(mymod)(5,6);
module.exports = function(a,b){
 return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};
main.js

module.exports = function(words){
    console.log(words.a + ' ' + words.b);
};
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
var obj = require(mymod)(5,6);
module.exports = function(a,b){
 return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};

您还可以在require:)中删除.js。

问题是:您想要实现什么

如果只想导出一个静态函数,可以使用。如果您想在exports属性中存储对象并从require caching node中获益,js提供了一种(脏的)方法。我想这就是你尝试过的

在Web浏览器上下文中使用JavaScript,可以使用
窗口
对象存储全局值。节点仅为所有模块提供一个全局对象:
过程
对象:

main.js

module.exports = function(words){
    console.log(words.a + ' ' + words.b);
};
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
var obj = require(mymod)(5,6);
module.exports = function(a,b){
 return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};
mymod.js

module.exports = function(words){
    console.log(words.a + ' ' + words.b);
};
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
var obj = require(mymod)(5,6);
module.exports = function(a,b){
 return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};
或者,如果您对导出缓存不感兴趣,可以这样做:

main.js

module.exports = function(words){
    console.log(words.a + ' ' + words.b);
};
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
var obj = require(mymod)(5,6);
module.exports = function(a,b){
 return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};
mymod.js

module.exports = function(words){
    console.log(words.a + ' ' + words.b);
};
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
var obj = require(mymod)(5,6);
module.exports = function(a,b){
 return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};

这应该可以工作,但有点脏:\n没有module.exports函数是不可能的吗?不幸的是,我不知道。我希望有人能提供一种更干净的方法,但我对require的理解是,一切都是模块化的。因此,当您需要在模块外部使用函数时,您必须将其“导出”到应用程序中。
“那么,没有全局变量是否可能?”