Javascript 如何从另一个require()文件引用一个require()文件中的函数?
这是我第二个周末玩Node,所以这是一个新手 我有一个js文件,里面有很多常用的实用程序,它们提供了JavaScript所没有的东西。严重剪裁,看起来像这样:Javascript 如何从另一个require()文件引用一个require()文件中的函数?,javascript,node.js,Javascript,Node.js,这是我第二个周末玩Node,所以这是一个新手 我有一个js文件,里面有很多常用的实用程序,它们提供了JavaScript所没有的东西。严重剪裁,看起来像这样: module.exports = { Round: function(num, dec) { return Math.round(num * Math.pow(10,dec)) / Math.pow(10,dec); } }; module.exports = { Init: function(p
module.exports = {
Round: function(num, dec) {
return Math.round(num * Math.pow(10,dec)) / Math.pow(10,dec);
}
};
module.exports = {
Init: function(pie) {
// does lots of other stuff, but now needs to round a number
// using the custom rounding fn provided in the common util code
console.log(util.Round(pie, 2)); // ReferenceError: util is not defined
}
};
var util = require("./utilities.js");
var customCode = require("./programCode.js");
customCode.Init(Math.PI);
许多其他自定义代码模块(也通过require()语句包含)需要调用实用程序函数。他们打这样的电话:
module.exports = {
Round: function(num, dec) {
return Math.round(num * Math.pow(10,dec)) / Math.pow(10,dec);
}
};
module.exports = {
Init: function(pie) {
// does lots of other stuff, but now needs to round a number
// using the custom rounding fn provided in the common util code
console.log(util.Round(pie, 2)); // ReferenceError: util is not defined
}
};
var util = require("./utilities.js");
var customCode = require("./programCode.js");
customCode.Init(Math.PI);
实际运行的node.js文件非常简单(对于本例来说)。它只需要在代码中包含()并启动自定义代码的Init()fn,如下所示:
module.exports = {
Round: function(num, dec) {
return Math.round(num * Math.pow(10,dec)) / Math.pow(10,dec);
}
};
module.exports = {
Init: function(pie) {
// does lots of other stuff, but now needs to round a number
// using the custom rounding fn provided in the common util code
console.log(util.Round(pie, 2)); // ReferenceError: util is not defined
}
};
var util = require("./utilities.js");
var customCode = require("./programCode.js");
customCode.Init(Math.PI);
这不起作用,我从customCode中得到一个“ReferenceError:util未定义”。我知道每个必需文件中的所有内容都是“私有”的,这就是发生错误的原因,但我也知道持有实用程序代码对象的变量必须存储在某个地方,可能挂起了全局
我搜索了global
,但在那里没有看到任何对utils
的引用。我正在考虑在自定义代码中使用类似于global.utils.Round的东西
因此,问题是,鉴于实用程序代码可以被称为任何真正的东西(var u、util或utility),我该如何组织它,以便其他代码模块可以看到这些实用程序?至少有两种方法可以解决这一问题:
如果您需要文件中另一个模块的内容,只需要它。这很简单
提供一些实际为您构建模块的内容。我马上就解释
但是,您当前的方法不起作用,因为node.js模块系统没有提供您从其他语言可能期望的全局语言。除了使用module.exports导出的内容外,您从所需的模块中什么也得不到,并且所需的模块对requiree的环境一无所知
只需要求它
为了避免上述差距,您需要事先要求其他模块:
// -- file: ./programCode.js
var util = require(...);
module.exports = {
Init: function(pie) {
console.log(util.Round(pie, 2));
}
};
require
s是缓存的,所以此时不要过多考虑性能
保持灵活性
在这种情况下,您不会直接导出模块的内容。相反,您提供了一个将创建实际内容的构造函数。这使您能够提供一些附加参数,例如实用程序库的另一个版本:
// -- file: ./programCode.js
module.exports = {
create: function(util){
return {
Init: function(pie) {
console.log(util.Round(pie, 2));
}
}
}
};
// --- other file
var util = require(...);
var myModule = require('./module').create(util);
如您所见,当您调用create
时,这将创建一个新对象。因此,作为第一种方法,它将消耗更多内存。因此,我建议您只需要require()
东西。至少有两种方法可以解决这个问题:
如果您需要文件中另一个模块的内容,只需要它。这很简单
提供一些实际为您构建模块的内容。我马上就解释
但是,您当前的方法不起作用,因为node.js模块系统没有提供您从其他语言可能期望的全局语言。除了使用module.exports导出的内容外,您从所需的模块中什么也得不到,并且所需的模块对requiree的环境一无所知
只需要求它
为了避免上述差距,您需要事先要求其他模块:
// -- file: ./programCode.js
var util = require(...);
module.exports = {
Init: function(pie) {
console.log(util.Round(pie, 2));
}
};
require
s是缓存的,所以此时不要过多考虑性能
保持灵活性
在这种情况下,您不会直接导出模块的内容。相反,您提供了一个将创建实际内容的构造函数。这使您能够提供一些附加参数,例如实用程序库的另一个版本:
// -- file: ./programCode.js
module.exports = {
create: function(util){
return {
Init: function(pie) {
console.log(util.Round(pie, 2));
}
}
}
};
// --- other file
var util = require(...);
var myModule = require('./module').create(util);
如您所见,当您调用create
时,这将创建一个新对象。因此,作为第一种方法,它将消耗更多内存。因此,我建议您只需要require()
things。多次需要同一个库,这是肯定的。由于您提到的原因(担心重复和性能),这不是直观的。我已经对此进行了测试,效果良好,因此感谢您的回答和解释@GeekStocks:通常我会看一看你的公司,但在你的情况下,我只是假设你期望某种全球管理。require
系统更像Python或Haskell的import
或C/C++#include
(但在这两种情况下都是隐藏的,只要你不再导出这些东西,这会导致你的问题-这是其他系统的不同之处)。多次需要同一个库并不是我看到的结果,那是肯定的。由于您提到的原因(担心重复和性能),这不是直观的。我已经对此进行了测试,效果良好,因此感谢您的回答和解释@GeekStocks:通常我会看一看你的公司,但在你的情况下,我只是假设你期望某种全球管理。require
系统更像Python或Haskell的import
或C/C++#include
(但在这两种情况下都是隐藏的,只要你不再导出这些东西,这会导致你的问题——这就是其他系统的区别)。