Node 模块

栏目: Node.js · 发布时间: 6年前

内容简介:模块,是 Node 让代码易于重用的一种组织和包装方式

Node.js ® is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Current Version: v0.10.33

为什么要有 Node 模块?

模块,是 Node 让代码易于重用的一种组织和包装方式

模块创建流程

创建模块

var a = 'a';
function A() {
    console.log(a);
}
exports.printA = A;

引入模块

var a = require('./module_');
a.printA();

模块暴露构造函数

//定义
var B = function (input) {
    this.input = input;
}
B.prototype.printB = function () {
    console.log(this.input);
}
module.exports = exports = B;

//调用
var B = require('./module_');
var b = new B('asdf');
b.printB();
  • exports
    只是对 module.exports 的一个全局引用,最初被定义为一个可以添加属性的空对象
  • exports.printA
    是 module.exports.printA 的简写
  • exports = B
    将会打破 module.exports 和 exports 之间的引用关系
  • module.exports = exports
    可以修复链接

Module 加载流程

Node 模块

目录模块

Node 模块

Monkey Patching

Node 将模块作为对象缓存起来

第一个文件会将模块返回的数据存到程序的内存中,第二个文件就不用再去访问和计算模块的源文件了

并且第二次引入有机会修改缓存的数据

欢迎加入我们的技术群,一起交流学习

人工智能 (高级)& (进阶)| BigData | 算法


以上所述就是小编给大家介绍的《Node 模块》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Domain-Driven Design Distilled

Domain-Driven Design Distilled

Vaughn Vernon / Addison-Wesley Professional / 2016-6-2 / USD 36.99

Domain-Driven Design (DDD) software modeling delivers powerful results in practice, not just in theory, which is why developers worldwide are rapidly moving to adopt it. Now, for the first time, there......一起来看看 《Domain-Driven Design Distilled》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具