Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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 节点模块之外的JS功能范围_Node.js - Fatal编程技术网

Node.js 节点模块之外的JS功能范围

Node.js 节点模块之外的JS功能范围,node.js,Node.js,我知道,在Node中,如果在module.exports之外定义了变量,那么它仍然是本地变量,这意味着它不会对全局名称空间产生冲突。这不是公开的。什么是公共的是模块中定义的 然而,功能又如何呢。它们的行为与变量相同吗?存在于module.exports外部的函数名会发生冲突吗 例如: myFirst.js var iAmStillPrivate = "private"; module.exports = { ...whatever code I wanna expose through thi

我知道,在Node中,如果在module.exports之外定义了变量,那么它仍然是本地变量,这意味着它不会对全局名称空间产生冲突。这不是公开的。什么是公共的是模块中定义的

然而,功能又如何呢。它们的行为与变量相同吗?存在于module.exports外部的函数名会发生冲突吗

例如:

myFirst.js

var iAmStillPrivate = "private";

module.exports = {
...whatever code I wanna expose through this module
}

function find(somethingId)
{

    ..some code
};
var iAmStillPrivate = "private";

module.exports = {
...whatever code
}

function find(somethingId)
{

    ..some code
};
var iAmStillPrivate = "private";

module.exports = {
     find: find(id)
...whatever code
}

var find = function find(somethingId)
{

    ..some code
};
mySecond.js

var iAmStillPrivate = "private";

module.exports = {
...whatever code I wanna expose through this module
}

function find(somethingId)
{

    ..some code
};
var iAmStillPrivate = "private";

module.exports = {
...whatever code
}

function find(somethingId)
{

    ..some code
};
var iAmStillPrivate = "private";

module.exports = {
     find: find(id)
...whatever code
}

var find = function find(somethingId)
{

    ..some code
};
find()是否冲突并污染了全局命名空间

我应该这样做吗

mySecond.js

var iAmStillPrivate = "private";

module.exports = {
...whatever code I wanna expose through this module
}

function find(somethingId)
{

    ..some code
};
var iAmStillPrivate = "private";

module.exports = {
...whatever code
}

function find(somethingId)
{

    ..some code
};
var iAmStillPrivate = "private";

module.exports = {
     find: find(id)
...whatever code
}

var find = function find(somethingId)
{

    ..some code
};
或者把它放到一个变量里没有关系吗?良好做法?没关系吧

结论

mySecond.js(我是一个母函数模块。我创建了一个隐式匿名函数,当我“需要”在其他.js文件中时,它将所有内容都封装在这里)

(module.exports-我允许这些内容是公共的。当我在node中说public时,也就是说…公共的,因为这里的内容可以被其他.js文件或其他任何文件中的其他模块访问。)

(我仍然是模块作用域内的函数,但尚未导出,因此其他模块无法使用我,我只属于该模块(根匿名函数)


NodeJ的每个模块中的函数都是该模块的本地函数,并且不会与同名的其他模块的函数冲突。 想象一下,模块被包装在一个函数中

此外,您确实不需要为函数定义变量,因为最终这并不重要,这两行函数和变量的范围对于模块是相同的和局部的:

var find = function find(somethingId) ...

function find(somethingId) ...
附带问题 此外,在您的评论中,您询问了该场景:

如果我在全局范围内有一个函数,而一个模块也有一个同名的私有函数,那该怎么办?它们是否冲突

发生的情况是,在您的模块内,对该函数的任何调用都将触发
本地
函数,而不是
全局
函数。一旦您在该模块外或其他模块内,对该函数的任何调用都将触发
全局
函数

让我们用一个例子来看一下。假设我们的节点应用程序的起点是
index.js
,这里是它的内容:

echo('one');

require('./include.js')

echo('three');

function echo(txt){
  console.log("we are in index.js", txt);
}
下面是一个名为
include.js
的模块:

echo('two');

function echo(txt){
  console.log("we are in include.js", txt);
}
如果使用
node index.js
命令运行此应用程序,则输出应为:

we are in index.js one
we are in include.js two
we are in index.js three

看到了吗?正如我前面解释的,所有的函数都在那里工作。

模块内的函数就是模块内的函数。你没有明白我的问题。我想我明白了。我想你没有明白模块是什么。文件是module.module.exports是模块定义,这是导入的…不是.js文件,意思是节点模块。.js文件只是一个包含您需要的代码的文件。导入的是module.exports值。我们需要啤酒图标。同样,在我最后的评论之后,您能检查我的结论吗,请参阅更新的帖子。让我们来看看。