Node 模块

作者: 宇宙湾 | 来源:发表于2019-05-13 11:25 被阅读0次

Node.js 是什么?

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
     可以修复链接

Monkey Patching

Node 将模块作为对象缓存起来
 第一个文件会将模块返回的数据存到程序的内存中,第二个文件就不用再去访问和计算模块的源文件了
 并且第二次引入有机会修改缓存的数据

欢迎直接访问我的个人博客,阅读效果更佳:https://yuzhouwan.com/posts/23363/

相关文章

  • 01-Node 基础使用

    Node 基础使用Node 介绍Node 模块化开发模块成员的导出模块成员的导入Node 系统模块 path 和 ...

  • node模块载入机制

    node内模块以及载入顺序为: 内置模块 文件模块 文件目录模块 node_modules模块 内置模块 http...

  • Koa系列1:Koa中使用mysql模块操作数据库

    安装 node.js的mysql模块 1.模块介绍 mysql模块是node操作MySQL的引擎,可以在node....

  • Node.js 核心模块概述

    模块加载原理与加载方式 Node 中的模块:核心模块/原生模块:Node提供的模块。文件模块:用户编写的模块。 N...

  • 2018-08-20第五天课

    内置模块 => 直接使用 Node 提供好的核心模块 Event 事件模块事件模块是整个 Node.js ...

  • 关于node.js一些模块的记录「FS模块」

    目录 Node.JS教程 FS模块 Path模块 FS模块 Path模块 Node.js path 模块提供了一些...

  • 04-文件读写

    fs模块---》操作文件---》io----》node的特长 fs模块是node非常重要的模块,能体现出node的...

  • node工具模块

    Node.js工具模块node工具模块分为OS,Path, Net, DNS, Domain模块 OS 字节顺序 ...

  • Node.js基础-模块

    Node中的JavaScript ECMAScript 核心模块 第三方模块 用户自定义模块 核心模块 Node为...

  • Node模块原理0926

    Node模块原理 1.node模块原理分析 js代码 2.node模块加载分析(多看几遍视频) 3.自己实现一下(...

网友评论

    本文标题:Node 模块

    本文链接:https://www.haomeiwen.com/subject/fmklaqtx.html