内容简介:模块,是 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 加载流程
目录模块
Monkey Patching
Node 将模块作为对象缓存起来
第一个文件会将模块返回的数据存到程序的内存中,第二个文件就不用再去访问和计算模块的源文件了
并且第二次引入有机会修改缓存的数据
欢迎加入我们的技术群,一起交流学习
人工智能 (高级)& (进阶)| BigData | 算法
以上所述就是小编给大家介绍的《Node 模块》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Node.js模块系统 (创建模块与加载模块)
- 黑客基础,Metasploit模块简介,渗透攻击模块、攻击载荷模块
- 022.Python模块序列化模块(json,pickle)和math模块
- 024.Python模块OS模块
- 023.Python的随机模块和时间模块
- Laravel 模块化开发模块 – Caffienate
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
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》 这本书的介绍吧!