- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一件事我不太理解 module.exports 和 NodeJS 的 require() 。由于我无法正确解释,因此找不到我要查找的信息,因此我来这里寻求一些帮助来寻找答案。
我可以导出如下函数:
example1.js
module.exports = {
hello: function() {
return 'Hello world';
});
}
或者
example2.js
module.exports = function(data) {
return data;
}
我可以将它与类似的东西一起使用:
console.log(require('./example1.js').hello());
或者
console.log(require('./example2.js')('hello world'));
从那里开始,如果我在 server.js
中调用 NPM 模块,如下所示:var bcrypt = require('bcrypt');
并且我想在另一个模块中使用它,最好将它传递给如下函数:
example3.js
module.exports = function(bcrypt) {
return bcrypt.hashSync('hello', 5);
}
之后(记录哈希值):
console.log(require(./example3.js)(bcrypt));
或者直接在模块中声明它?
example3bis.js
var bcrypt = require('bcrypt');
module.exports = function() {
return bcrypt.hashSync('hello', 5);
}
另一个问题,如果我在 server.js
和 example3bis.js
等特定模块中声明 bcrypt,是否意味着我在复制代码?
最后,模块是否有可遵循的良好实践?
我不知道我是否清楚地解释了事情,但我尝试理解模块(npm 或导出)周围的事情,以及如果多次调用代码,代码是如何重复的。
预先感谢您的回答!
最佳答案
好的,这就是答案
require
文件中正在使用该模块的模块,不要传递它。
require
只是维护已加载模块的缓存,因此多次 require
模块不会有任何性能损失,并且没有代码也有重复
所以一般来说,根本不需要担心代码重复或性能问题,这些已经在模块系统中考虑到了,只需在需要它的文件中需要模块,而不是作为参数传递,这会导致更干净的代码
关于javascript - 操作 module.exports 和 require,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006599/
在我的项目代码优化期间,我替换了 var 的所有实例关键词 let因为我认为 var 没有特别的用途.为此,我在所有文件中执行了“查找和替换”操作。 在一些自定义模块中,我使用了如下语句: var e
我遇到过使用“双”导出来创建当前组件的情况。您能否解释一下它是否有真正的用途,或者只是作者的偏好? import React from 'react' import DuckImage from '.
我编写了一个简单的脚本来创建用户 (TestV100)、在该架构中创建表 (Xy100) 并将制表符分隔的平面文件从 hadoop 导出到此 Oracle 表。 这是 shell 脚本:-Export
我真的很困惑: export const foo 导出默认 foo module.exports = foo; 我知道这些是非常基本的,但有人可以区分并向我解释这些。我真的很想明白。 最佳答案 让我们
今天我在我的 AngularJS 项目中采用了 Browserify,但是我还不清楚。在所有示例和博客文章中,我都看到了这样的东西: /app.js: require('./messages'); a
我正在寻找一种方法: 隐藏 HTML 页面结果上的标题 导出时在 highcharts 图表上显示标题(PDF、PNG、JPEG 或打印) 我不知道如何继续。有人可以帮助我吗? 最佳答案 您可以在导出
在我的应用程序的首选项中,我调用以特殊方式处理某些参数的 Activity 。到目前为止它运作良好。但是,为了创建试用版和专业版,我刚刚创建了一个包含所有代码的库和一个调用该库的新演示应用程序。除了由
pentaho cde 导出组件正在为我工作,它导出表数据,文件名为 cda-export.csv,我可以自动设置文件名吗? 最佳答案 尝试使用参数 {filename : 'myfilename
我想从 Gephi 的预览选项卡中导出 SVG/PDF/PNG。导出的网络包括所有节点,但切断了左右两侧的节点标签。如何获得不切断标签的导出 pdf? 最佳答案 您必须通过控制选项 来调整输出的大小。
如标题所述,我在尝试使用我的 perl 模块时遇到此错误,但我不知道它是什么意思,而且我似乎无法在 Internet 上找到任何明确的结果。我的代码由 3 个文件组成:一个脚本 (myApp.pl),
我曾经每天下载一个 firebase 实时数据库的节点,通过导出该节点的 .JSON 文件来监控一些输出。 JSON 文件本身大约 8MB。 最近,我开始收到一个错误: “导出 JSON 无法导出在单
之间有什么区别 export * as bar from 'foo' 和 export { default as bar } from 'foo' 在我的特殊情况下,我尝试了以下两种方法,它们都有效,
我想我要求的是不可能的OOB,但我想确认一下。 我们正在升级到 ES6(使用 Babel)。项目是一个网站,使用 AMD (requirejs)。我想将实用程序模块 (foolib) 转换为 ES6,
我一直在我的 React 中广泛使用命名导出和默认导出,我遇到了这 2 个相似的语法。 从'./Button'导出默认值; export { default } from './Button'; 有人
在此页面 ( http://docs.nodejitsu.com/articles/getting-started/what-is-require ) 上,它指出“如果要将导出对象设置为函数或新对象,
在此页面 ( http://docs.nodejitsu.com/articles/getting-started/what-is-require ) 上,它指出“如果要将导出对象设置为函数或新对象,
我在运行 flow check 时收到此错误,但我不确定这是什么意思。 Cannot use exports as a type because exports is a value. To get
module.export和export有什么区别? 如果module.export对象中有一些属性怎么办?export.xx 会无效吗? 最佳答案 首先是 exports 和 module.expo
我正在学习 typescript 。在研究一些源文件时,我发现声明文件 .d 中有时有 export declare ... 有时只有 export ... .ts. 经过一些测试,在我看来它们是等价
假设我有一个要导出的变量。有什么区别 export const a = 1; 对比 export let a = 1; 我理解const 和let 之间的区别,但是当你导出它们时,有什么区别? 最佳答
我是一名优秀的程序员,十分优秀!