Javascript NodeJS-要求和模块
NodeJS中的和是否可以用于获取驻留在目录中的所有JavaScript文件中的所有函数,而不是单个JavaScript文件中的所有函数Javascript NodeJS-要求和模块,javascript,node.js,prototypal-inheritance,Javascript,Node.js,Prototypal Inheritance,NodeJS中的和是否可以用于获取驻留在目录中的所有JavaScript文件中的所有函数,而不是单个JavaScript文件中的所有函数如果是,如何解释?任何人都可以使用require用一个示例来解释它吗 您需要该文件中的模块,并且可以使用该原型的所有功能(单个文件),而不是完整的目录。 e、 g 您可以需要此模块,也可以使用“帮助”功能 通过这种方法,您可以在单个文件中要求所有文件(模块):“Node.js是一种用于开发服务器端Web应用程序的开源、跨平台运行时环境 尽管Node.js不是Ja
如果是,如何解释?
任何人都可以使用require用一个示例来解释它吗 您需要该文件中的模块,并且可以使用该原型的所有功能(单个文件),而不是完整的目录。 e、 g 您可以需要此模块,也可以使用“帮助”功能 通过这种方法,您可以在单个文件中要求所有文件(模块):“Node.js是一种用于开发服务器端Web应用程序的开源、跨平台运行时环境 尽管Node.js不是JavaScript框架,但它的许多基本模块都是用JavaScript编写的,开发人员可以用JavaScript编写新模块 运行时环境使用谷歌的V8 JavaScript引擎解释JavaScript。” Nodejs示例: 您有Afile.js
var Afile = function()
{
};
Afile.prototype.functionA = function()
{
return 'this is Afile';
}
module.exports = Afile;
和Bfile.js
var Bfile = function()
{
};
Bfile.prototype.functionB = function()
{
return 'this is Bfile';
}
module.exports = Bfile;
var Afile = require(__dirname + '/Afile.js');
var Bfile = require(__dirname + '/Bfile.js');
var Test = function()
{
};
Test.prototype.start = function()
{
var Afile = new Afile();
var Bfile = new Bfile();
Afile.functionA();
Bfile.functionB();
}
var test = Test;
test.start();
Test.js文件需要Afile.js和Bfile.js
var Bfile = function()
{
};
Bfile.prototype.functionB = function()
{
return 'this is Bfile';
}
module.exports = Bfile;
var Afile = require(__dirname + '/Afile.js');
var Bfile = require(__dirname + '/Bfile.js');
var Test = function()
{
};
Test.prototype.start = function()
{
var Afile = new Afile();
var Bfile = new Bfile();
Afile.functionA();
Bfile.functionB();
}
var test = Test;
test.start();
如果require给定了目录路径,它将在该目录中查找index.js文件。因此,将特定于模块的js文件放在一个目录中,创建index.js文件&最后需要工作js文件中的该目录。希望下面的例子能帮助 例如: 文件:modules/moduleA.js
function A (msg) {
this.message = msg;
}
module.exports = A;
文件:modules/moduleB.js
function B (num) {
this.number = num;
}
module.exports = B;
文件:modules/index.js
module.exports.A = require("./moduleA.js");
module.exports.B = require("./moduleB.js");
文件:test.js
var modules = require("./modules");
var myMsg = new modules.A("hello");
var myNum = new modules.B("000");
console.log(myMsg.message);
console.log(myNum.number);
可能重复的