当前位置:网站首页>nodejs笔记3
nodejs笔记3
2022-04-22 20:57:00 【Revin050】
文章目录
模块化
模块化是指解决一个复杂问题时,自顶向下逐层把系统划分成若干模块的过程。对于整个系统来说,模块是可组合、分解和更换的单元。
编程领域中的模块化,就是遵守固定的规则,把一个大文件拆成独立并互相依赖的多个小模块。
把代码进行模块化拆分的好处:
① 提高了代码的复用性
② 提高了代码的可维护性
③ 可以实现按需加载
Node.js 中模块的分类
Node.js 中根据模块来源的不同,将模块分为了 3 大类,分别是:
内置模块(内置模块是由 Node.js 官方提供的,例如 fs、path、http 等)
自定义模块(用户创建的每个 .js 文件,都是自定义模块)
第三方模块(由第三方开发出来的模块,并非官方提供的内置模块,也不是用户创建的自定义模块,使用前需要先下载)
加载模块
使用强大的 require() 方法,可以加载需要的内置模块、用户自定义模块、第三方模块进行使用。例如:

注意:使用 require() 方法加载其它模块时,会执行被加载模块中的代码。
Node.js 中的模块作用域
什么是模块作用域
和函数作用域类似,在自定义模块中定义的变量、方法等成员,只能在当前模块内被访问,这种模块级别的访问限制,叫做模块作用域。
模块作用域的好处
防止了全局变量污染的问题
module 对象
在每个 .js 自定义模块中都有一个 module 对象,它里面存储了和当前模块有关的信息,打印如下:

module.exports 对象
在自定义模块中,可以使用 module.exports 对象,将模块内的成员共享出去,供外界使用。
外界用 require() 方法导入自定义模块时,得到的就是 module.exports 所指向的对象。
在暴露前,输出为空

暴露后就可以拿到模块里面的内容


共享成员时的注意点
使用 require() 方法导入模块时,导入的结果,永远以 module.exports 指向的对象为准。当module.exports指向新对象时,旧对象就自动消失了,如下所示。此时只会暴露nickname与sayHi()

exports 对象
由于 module.exports 单词写起来比较复杂,为了简化向外共享成员的代码,Node 提供了 exports 对象。默认情况下,exports 和 module.exports 指向同一个对象。
最终共享的结果,还是以module.exports 指向的对象为准。

exports 和 module.exports 的使用误区
时刻谨记,require() 模块时,得到的永远是 module.exports 指向的对象:

注意:为了防止混乱,最好不要在同一个模块中同时使用 exports 和 module.exports
npm与包
概念
Node.js 中的第三方模块又叫做包。
就像电脑和计算机指的是相同的东西,第三方模块和包指的是同一个概念,只不过叫法不同。
包的来源
不同于 Node.js 中的内置模块与自定义模块,包是由第三方个人或团队开发出来的,免费供所有人使用。
注意:Node.js 中的包都是免费且开源的,不需要付费即可免费下载使用。
npm的作用
可以用来安装包
npm install 包的名称
npm i 包的名称
使用npm安装包与不使用包的区别
不使用包实现时间格式化的效果如下所示。

使用包实现时间格式化的效果如下所示。首先安装对应的包,接下来直接导入使用即可。

版权声明
本文为[Revin050]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45847016/article/details/124330745
边栏推荐
- Programming utilities, there is always one you use
- Huawei computer test question - hj61 put apple
- DSPACE仿真平台的使用
- 二、线性回归
- [server data recovery] a data recovery case in which multiple hard disks are disconnected and the server crashes due to water ingress in the server
- - 4. Compare strings (10 points) the C language standard function library includes the StrCmp function for string comparison. As an exercise, we write a function with the same function.
- H. Maximal AND
- Short link design and thinking
- After working for three years as a programmer in a small 9K factory with a monthly salary, he finally won the byte offer he had been longing for for for for a long time in his spare time. The salary c
- 2022年危险化学品经营单位安全管理人员上岗证题库及模拟考试
猜你喜欢

L1-046 divide singles

C # convert ofd to PDF
Reprint: the development direction of programmers

紫光国微:正积极拓展模拟芯片品类,部分产品已实现批量销售
October's Android interview failed miserably in byte three, and fortunately won Xiaomi offer

大量mapper IO优化(使用多线程异步+CountDownLatch)

2、 Linear regression

Summary of unauthorized access vulnerabilities

Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst

显示实现接口和隐式实现接口的区别
随机推荐
Huawei machine test questions summary
Write ppt with markdown
Da14580 sends data as server
How does redis use the command to clear all keys
Summary of unauthorized access vulnerabilities
Building a new generation of computing platform, stepvr will open the "door" of metauniverse in 2022
MySQL开发技巧
buuctf-[Flask]SSTI
L1-046 整除光棍
Matlab learning notes - calculate eigenvectors and manually execute PCA
buuctf-[Flask]SSTI
一、机器学习概念
转载:程序员的发展方向
LeetCode刷题日记 - 15. 三数之和
Huawei machine test question - hj62 find the number of 1 in the input integer binary
Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst
MySQL learning notes
- 4. Compare strings (10 points) the C language standard function library includes the StrCmp function for string comparison. As an exercise, we write a function with the same function.
基于GD32F1x0手动编程实现简易freertos之任务阻塞延时
DSPACE仿真平台的使用