GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.runoob.com — Node.js 常用工具 | 菜鸟教程
Node.js 教程
Node.js 教程
Node.js 概念
Node.js 安装配置
Node.js VS Code
Node.js NVM
Node.js 创建第一个应用
Node.js 工作机制
NPM 使用介绍
PNPM 使用介绍
Node.js REPL
Node.js 异步编程
Node.js 回调函数
Node.js Promise
Node.js async/await
Node.js 事件循环
Node.js EventEmitter
Node.js Buffer
Node.js Stream
Node.js 模块系统
Node.js 模块导入
Node.js 函数
Node.js 路由
Node.js 全局对象
Node.js 常用工具
Node.js 文件系统
Node.js 内置模块
Node.js 测验
Node.js 应用Node.js 构建简单应用 Node.js GET/POST请求 Node.js util 模块 Node.js Web 模块 Node.js Express 框架 Node.js RESTful API Node.js 多进程 Node.js JXcore 打包 Node.js MySQL Node.js MongoDB npx 入门教程Node.js 常用工具util 模块是 Node.js 的一个内置模块,包含了实用工具函数,用于支持 JavaScript 编程中的调试、错误处理、格式化等功能。 util 提供常用函数的集合,用于弥补核心 JavaScript 的功能过于精简的不足。 util 模块中的功能涵盖了从对象检查、继承到格式化字符串等多个方面。 导入 util 模块首先,你需要导入 util 模块: const util = require('util');
常用方法:
util.types 下的类型检测方法util.types 是一个包含许多类型检测方法的集合,扩展了 JavaScript 的 typeof 和 instanceof。
应用实例util.format() - 字符串格式化util.format() 用于生成格式化字符串,支持占位符如 %s、%d 和 %j,分别代表字符串、数字和 JSON。 实例
const util = require('util'); const name = 'Alice'; const age = 25; console.log(util.format('Name: %s, Age: %d', name, age)); // 输出: Name: Alice, Age: 25 util.promisify() - 转换回调函数为 Promiseutil.promisify() 将传统回调风格的函数转换为返回 Promise 的函数,从而可以与 async/await 一起使用。 实例
const util = require('util'); const fs = require('fs'); // 将回调风格的 fs.readFile 转换为返回 Promise 的函数 const readFileAsync = util.promisify(fs.readFile); (async () => { try { const data = await readFileAsync('example.txt', 'utf8'); console.log(data); } catch (err) { console.error(err); } })(); util.callbackify() - 将 Promise 转换为回调util.callbackify() 将返回 Promise 的函数转换为回调风格的函数,便于在需要回调的代码环境中使用。 util.callbackify(original) 将 实例
const util = require('util'); async function fn() { return 'hello world'; } const callbackFunction = util.callbackify(fn); callbackFunction((err, ret) => { if (err) throw err; console.log(ret); }); 以上代码输出结果为: hello world 回调函数是异步执行的,并且有异常堆栈错误追踪。 如果回调函数抛出一个异常,进程会触发一个 'uncaughtException' 异常,如果没有被捕获,进程将会退出。 null 在回调函数中作为一个参数有其特殊的意义,如果回调函数的首个参数为 Promise 拒绝的原因且带有返回值,且值可以转换成布尔值 false,这个值会被封装在 Error 对象里,可以通过属性 reason 获取。 function fn() {
return Promise.reject(null);
}
const callbackFunction = util.callbackify(fn);
callbackFunction((err, ret) => {
// 当 Promise 被以 `null` 拒绝时,它被包装为 Error 并且原始值存储在 `reason` 中。
err && err.hasOwnProperty('reason') && err.reason === null; // true
});
original 为 async 异步函数。该函数返回传统回调函数。 util.deprecate() - 标记函数为废弃util.deprecate() 用于标记不推荐使用的函数,调用时会显示警告信息。 实例
const util = require('util'); const oldFunction = util.deprecate(() => { console.log('This function is deprecated'); }, 'oldFunction is deprecated. Use newFunction instead.'); oldFunction(); // 调用时会显示警告 util.types - 数据类型检测util.types 提供了一组方法来检测特定数据类型,扩展了 typeof 和 instanceof 的功能。 实例
const util = require('util'); console.log(util.types.isDate(new Date())); // true console.log(util.types.isMap(new Map())); // true util.inheritsutil.inherits(constructor, superConstructor) 是一个实现对象间原型继承的函数。 JavaScript 的面向对象特性是基于原型的,与常见的基于类的不同。JavaScript 没有提供对象继承的语言级别特性,而是通过原型复制来实现的。 在这里我们只介绍 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);
var objBase = new Base();
objBase.showName();
objBase.sayHello();
console.log(objBase);
var objSub = new Sub();
objSub.showName();
//objSub.sayHello();
console.log(objSub);
我们定义了一个基础对象 Base 和一个继承自 Base 的 Sub,Base 有三个在构造函数内定义的属性和一个原型中定义的函数,通过util.inherits 实现继承。运行结果如下: base
Hello base
{ name: 'base', base: 1991, sayHello: [Function] }
sub
{ name: 'sub' }
注意:Sub 仅仅继承了Base 在原型中定义的函数,而构造函数内部创造的 base 属 性和 sayHello 函数都没有被 Sub 继承。 同时,在原型中定义的属性不会被 console.log 作 为对象的属性输出。如果我们去掉 objSub.sayHello(); 这行的注释,将会看到: node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: Object #<Sub> has no method 'sayHello' at Object.<anonymous> (/home/byvoid/utilinherits.js:29:8) at Module._compile (module.js:441:26) at Object..js (module.js:459:10) at Module.load (module.js:348:31) at Function._load (module.js:308:12) at Array.0 (module.js:479:10) at EventEmitter._tickCallback (node.js:192:40)
util.inspect() - 打印对象结构util.inspect(object[, options]):将对象转换为字符串表示形式,便于调试。 util.inspect() 将对象转换为字符串形式,常用于调试,以查看对象的详细结构和属性。可以指定 options 参数来控制输出格式。 实例const obj = { a: 1, b: 2, c: { d: 3 } }; console.log(util.inspect(obj, { showHidden: false, depth: null, colors: true })); util.isArray(object)如果给定的参数 "object" 是一个数组返回 true,否则返回 false。 var util = require('util');
util.isArray([])
// true
util.isArray(new Array)
// true
util.isArray({})
// false
util.isRegExp(object)如果给定的参数 "object" 是一个正则表达式返回true,否则返回false。 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。 var util = require('util');
util.isDate(new Date())
// true
util.isDate(Date())
// false (without 'new' returns a String)
util.isDate({})
// false
更多详情可以访问 http://nodejs.org/api/util.html 了解详细内容。
AI 思考中...
| ||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |