Node.js util 模块
util 模块的主要功能
1. 类型检查工具
util 模块提供了一些用于类型检查的函数,这些函数比 JavaScript 的原生 typeof 操作符更加精确和有用。
util.types.isArrayBuffer(value)
检查给定的值是否是 ArrayBuffer 实例。
示例代码
const arrBuffer = new ArrayBuffer(8);
console.log(util.types.isArrayBuffer(arrBuffer)); // true
console.log(util.types.isArrayBuffer([])); // false
util.types.isDate(value)
检查给定的值是否是 Date 实例。
示例代码
console.log(util.types.isDate(new Date())); // true
console.log(util.types.isDate('2023-01-01')); // false
2. 实用工具函数
util.format(format[, ...args])
类似于 C 语言中的 printf 函数,用于格式化字符串。
示例代码
const name = 'John';
const age = 30;
console.log(util.format('My name is %s and I am %d years old', name, age));
// 输出: My name is John and I am 30 years old
util.inspect(object[, options])
返回对象的字符串表示,主要用于调试。可以配置深度、颜色等选项。
示例代码
const obj = {
name: 'Alice',
details: {
age: 25,
hobbies: ['reading', 'coding']
}
};
console.log(util.inspect(obj, { depth: 2, colors: true }));
3. 回调风格转换
util.promisify(original)
将遵循 Node.js 回调风格的函数转换为返回 Promise 的函数。
示例代码
const fs = require('fs');
const readFile = util.promisify(fs.readFile);
async function readConfig() {
try {
const data = await readFile('config.json', 'utf8');
console.log(data);
} catch (err) {
console.error('Error reading file:', err);
}
}
readConfig();
util.callbackify(original)
与 promisify 相反,将返回 Promise 的函数转换为回调风格的函数。
示例代码
async function asyncFunc() {
return 'Hello World';
}
const callbackFunc = util.callbackify(asyncFunc);
callbackFunc((err, result) => {
if (err) throw err;
console.log(result); // Hello World
});
4. 继承工具
util.inherits(constructor, superConstructor)
实现原型继承(ES5 风格)。注意:在 ES6 中,建议使用 class 和 extends 关键字。
示例代码
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
console.log(this.name + ' makes a noise.');
};
function Dog(name) {
Animal.call(this, name);
}
util.inherits(Dog, Animal);
Dog.prototype.speak = function() {
console.log(this.name + ' barks.');
};
const d = new Dog('Rex');
d.speak(); // Rex barks.
util 模块的实际应用场景
1. 调试复杂对象
当需要打印复杂对象的结构时,util.inspect 比简单的 console.log 更加强大:
示例代码
const complexObj = {
date: new Date(),
regex: /test/g,
nested: {
array: [1, 2, 3],
fn: function() {}
}
};
console.log(util.inspect(complexObj, {
showHidden: true,
depth: null,
colors: true
}));
2. 现代化旧代码
将回调风格的旧代码转换为 Promise 风格:
示例代码
// 旧的回调风格代码
function oldStyleFunc(param, callback) {
// 一些异步操作
setTimeout(() => {
callback(null, `Result for ${param}`);
}, 100);
}
// 转换为 Promise 风格
const newStyleFunc = util.promisify(oldStyleFunc);
async function useNewStyle() {
const result = await newStyleFunc('test');
console.log(result); // Result for test
}
useNewStyle();
3. 自定义对象检查
可以自定义对象的 inspect 方法,改变 util.inspect 的输出:
示例代码
class CustomObject {
constructor(value) {
this.value = value;
}
[util.inspect.custom](depth, options) {
return `CustomObject: ${this.value}`;
}
}
const obj = new CustomObject('test');
console.log(util.inspect(obj)); // CustomObject: test
注意事项
- 性能考虑:
util.inspect对于大型对象可能会有性能影响,生产环境中应谨慎使用。 - API 稳定性:虽然
util模块是核心模块,但某些 API 可能仍会被标记为实验性功能。 - 现代替代方案:一些功能(如
util.inherits)在现代 JavaScript 中有更好的替代方案(如class语法)。 - 错误处理:使用
promisify时,确保正确处理错误,避免未捕获的 Promise 拒绝。
方法与属性
| 方法/属性 | 描述 | 版本引入 |
|---|---|---|
| util.callbackify(original) | 将返回 Promise 的 async 函数转换为回调风格的函数 | 8.2.0 |
| util.debuglog(section) | 创建一个只当 NODE_DEBUG 环境变量包含指定 section 时才记录消息的函数 | 0.11.3 |
| util.deprecate(fn, msg[, code]) | 包装函数,使其被调用时发出弃用警告 | 0.8.0 |
| util.format(format[, ...args]) | 使用第一个参数作为类似 printf 的格式字符串来格式化字符串 | 0.5.3 |
| util.formatWithOptions(inspectOptions, format[, ...args]) | 类似 util.format(),但接受 inspect 选项 | 10.0.0 |
| util.getSystemErrorName(err) | 返回由错误码 err 表示的系统错误名称 | 9.7.0 |
| util.inherits(constructor, superConstructor) | 将一个构造函数的原型方法继承到另一个构造函数(已弃用,建议使用 ES6 class 和 extends) | 0.3.0 |
| util.inspect(object[, options]) | 返回 object 的字符串表示,用于调试 | 0.3.0 |
| util.isDeepStrictEqual(val1, val2) | 测试两个值是否深度严格相等 | 9.0.0 |
| util.promisify(original) | 将回调风格的函数转换为返回 Promise 的函数 | 8.0.0 |
| util.stripVTControlCharacters(str) | 从字符串中剥离 ANSI 转义码 | 16.11.0 |
| util.TextDecoder | WHATWG 标准 TextDecoder 实现的别名 | 8.3.0 |
| util.TextEncoder | WHATWG 标准 TextEncoder 实现的别名 | 8.3.0 |
| util.types | 提供各种 Node.js 类型检查的工具对象 | 10.0.0 |
| util.aborted(err, signal) | 检查错误是否由中止信号引起 | 18.18.0 |
| util.parseArgs([config]) | 解析命令行参数的工具函数 | 18.3.0 |