博客
关于我
node.js模块、包
阅读量:795 次
发布时间:2023-02-16

本文共 1903 字,大约阅读时间需要 6 分钟。

创建模块

Node.js 提供了 exports 和 require 两个对

象,其中 exports 是模块公开的接口,require 用于从外部获取一个模块的接口,即所获
取模块的 exports 对象。
让我们以一个例子来了解模块。创建一个 module.js 的文件,内容是:
//module.js
 var name;
 exports.setName = function(thyName) {
  name = thyName;
};
 
exports.sayHello = function() {
  console.log('Hello ' + name);
};
在同一目录下创建 getmodule.js,内容是:
//getmodule.js
var myModule = require('./module');
myModule.setName('BYVoid');
myModule.sayHello();

运行node getmodule.js,结果是:
Hello BYVoid
在以上示例中,module.js 通过 exports 对象把 setName 和 sayHello 作为模块的访
问接口,在 getmodule.js 中通过 require('./module') 加载这个模块,然后就可以直接访
问 module.js 中 exports 对象的成员函数了。

覆盖exports

使用exports.Hello = Hello; 时,我们在其他文件中需要通过 require('./singleobject').Hello 来获取

Hello 对象,这略显冗余,可以用下面方法稍微简化:

可以通过module.exports = Hello;

包管理

npm i/install package安装本地包

npm i -g package安装全局包

npm link package source dest创建链接

npm init 初始化包的package.json

npm publish发布包

使用node-inspector调试

首先,使用 npm install -g node-inspector 命令安装 node-inspector,然后在终

端中通过 node --debug-brk=5858 debug.js 命令连接你要除错的脚本的调试服务器,
启动 node-inspector:
$ node-inspector
在浏览器中打开 http://127.0.0.1:8080/debug?port=5858,即可显示出优雅的 Web 调试工
具。

常用工具util

util.inherits(constructor, superConstructor)是一个实现对象间原型继承

的函数。JavaScript 的面向对象特性是基于原型的,与常见的基于类的不同。JavaScript 没有

提供对象继承的语言级别特性,而是通过原型复制来实现的,具体细节我们在附录A中讨论
在这里我们只介绍 util.inherits 的用法,示例如下:
var util = require('util');
 
function Base() {
  this.name = 'base';
  this.base = 1991;
   
  this.sayHello = function() {
    console.log('Hello ' + this.name);
  };
}
 
Base.prototype.showName = function() {
  console.log(this.name);

}; //原型定义

 function Sub() {
  this.name = 'sub';
}
 util.inherits(Sub, Base);

Base 有三个在构造函数内定义的属性和一个原型中定义的函数,通过 util.inherits 实现继承。

注意,Sub 仅仅继承了 Base 在原型中定义的函数,而构造函数内部创造的 base 属性和 sayHello 函数都没有被 Sub 继承。

util.inspect()将对象转化为字符串,其他util.isArray()、 util.isRegExp()、

util.isDate()、util.isError() 四个类型测试工具,以及 util.format()、util.
debug() 等工具

转载于:https://www.cnblogs.com/mebius4789/p/5023720.html

你可能感兴趣的文章
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>