Node.js其他模块
util
util 是一个 Node.js 核心模块,提供常用函数的集合,用于弥补核心 JavaScript 的功能 过于精简的不足
引入
1
const util = require('util');
util.callbackify
util.callbackify(original)
将 async
异步函数(或者一个返回值为 Promise
的函数)转换成遵循异常优先的回调风格的函数。
(err, value) => … 回调作为最后一个参数。 在回调函数中,第一个参数为拒绝的原因(如果 Promise 解决,则为 null),第二个参数则是解决的值。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const util = require('util');
async function fn() {
throw new Error('error test');
return 'hello world';
}
const callbackFunction = util.callbackify(fn);
callbackFunction((err, ret) => {
if (err) {
console.log('---> err: ', err.message);
throw err;
}
console.log('---> ret: ', ret);
});
util.inherits
util.inherits(constructor, superConstructor)
是一个实现对象间原型继承的函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var util = require('util');
function Base() {
this.name = 'base';
this.base = 1991;
this.sayHello = function () {
console.log('Base Hello ' + this.name);
};
}
Base.prototype.showName = function () {
console.log('Base showName ' + this.name)
};
function Sub() {
this.name = 'sub';
}
util.inherits(Sub, Base);
var objBase = new Base();
objBase.showName();
objBase.sayHello();
console.log("objBase: ", objBase);
var objSub = new Sub();
objSub.showName();
// objSub.sayHello(); // TypeError: objSub.sayHello is not a function
console.log("objSub: ", objSub);
结果:
Base showName base Base Hello base objBase: Base { name: ‘base’, base: 1991, sayHello:
[Function (anonymous)] }
Base showName sub objSub: Sub { name: ‘sub’ }
Sub 仅仅继承了 Base 在原型中定义的函数,而构造函数内部创造的 base 属性和 sayHello 函数都没有被 Sub 继承。
在原型中定义的属性不会被 console.log 作 为对象的属性输出。如果我们去掉 objSub.sayHello(); 这行的注释,将会看到:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> Base showName base
> Base Hello base
> objBase: Base { name: 'base', base: 1991, sayHello: `[Function (anonymous)] }`
> Base showName sub
> F:\Workspace\Node\nodejs\util\inherits_demo.js:25
> objSub.sayHello(); // TypeError: objSub.sayHello is not a function
> ^
> TypeError: objSub.sayHello is not a function
> at Object.<anonymous> (F:\Workspace\Node\nodejs\util\inherits_demo.js:25:8)
> at Module._compile (node:internal/modules/cjs/loader:1376:14)
> at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
> at Module.load (node:internal/modules/cjs/loader:1207:32)
> at Module._load (node:internal/modules/cjs/loader:1023:12)
> at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
> at node:internal/main/run_main_module:28:49
> Node.js v21.2.0
util.inspect
util.inspect(object,[showHidden],[depth],[colors])
是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出。
- 它至少接受一个参数 object,即要转换的对象。
- showHidden 是一个可选参数,如果值为 true,将会输出更多隐藏信息。
- depth 表示最大递归的层数,如果对象很复杂,你可以指定层数以控制输出信息的多 少。如果不指定 depth,默认会递归 2 层,指定为 null 表示将不限递归层数完整遍历对象。
- colors 如果 colors 值为 true,输出格式将会以 ANSI 颜色编码,通常用于在终端显示更漂亮 的效果。
特别要指出的是,util.inspect 并不会简单地直接把对象转换为字符串,即使该对 象定义了 toString 方法也不会调用。
示例:
1
2
3
4
5
6
7
8
9
10
var util = require('util');
function Person() {
this.name = 'byvoid';
this.toString = function (p1, p2) {
return this.name + ' ' + p1 + ' ' + p2;
};
}
var obj = new Person();
console.log(util.inspect(obj));
console.log(util.inspect(obj, true));
1
2
3
4
5
6
7
8
9
10
11
> Person { name: 'byvoid', toString: [Function (anonymous)] }
> Person {
> name: 'byvoid',
> toString: <ref *1> [Function (anonymous)] {
> [length]: 2,
> [name]: '',
> [arguments]: null,
> [caller]: null,
> [prototype]: { [constructor]: [Circular *1] }
> }
> }
util.isArray(object)
如果给定的参数 “object” 是一个数组返回 true,否则返回 false。
1
2
3
4
5
6
7
8
var util = require('util');
util.isArray([])
// true
util.isArray(new Array)
// true
util.isArray({})
// false
util.isRegExp(object)
如果给定的参数 “object” 是一个正则表达式返回 true,否则返回 false。
1
2
3
4
5
6
7
8
var util = require('util');
util.isRegExp(/some regexp/)
// true
util.isRegExp(new RegExp('another regexp'))
// true
util.isRegExp({})
// false
util.isDate(object)
如果给定的参数 “object” 是一个日期返回 true,否则返回 false。
1
2
3
4
5
6
7
8
var util = require('util');
util.isDate(new Date())
// true
util.isDate(Date())
// false (without 'new' returns a String)
util.isDate({})
// false