亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Node.js API詳解之 util模塊用法實(shí)例分析

 更新時(shí)間:2020年05月09日 08:41:44   作者:李小強(qiáng)  
這篇文章主要介紹了Node.js API詳解之 util模塊用法,結(jié)合實(shí)例形式分析了node.js API中util模塊基本功能與相關(guān)函數(shù)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Node.js API詳解之 util模塊用法。分享給大家供大家參考,具體如下:

Node.js API詳解之 util

util 模塊主要用于支持 Node.js 內(nèi)部 API 的需求。提供了大部分實(shí)用工具。
通過(guò) const util = require(‘util'); 的方式引用util模塊

util.callbackify(original)

說(shuō)明:

original:傳遞一個(gè) async 函數(shù),或者是一個(gè)返回Promise的異步函數(shù)。
callbackify會(huì)返回一個(gè)方法,執(zhí)行該方法時(shí)傳遞一個(gè)回調(diào)函數(shù),回調(diào)函數(shù)的第一個(gè)參數(shù)是err,第二個(gè)是異步函數(shù)的返回值。

demo:

const util = require('util');
async function fn() {
 return await Promise.resolve('hello isjs');
}
const callbackFunction = util.callbackify(fn);
callbackFunction((err, ret) => {
 if (err) throw err;
 console.log(ret);
});
//輸出: hello isjs

util.debuglog(section)

說(shuō)明:

util.debuglog() 方法用于創(chuàng)建一個(gè)函數(shù),基于 NODE_DEBUG 環(huán)境變量的存在與否有條件地寫入調(diào)試信息到 stderr。
如果 section 名稱在環(huán)境變量的值中,則返回的函數(shù)類似于 console.error()。 否則,返回的函數(shù)是一個(gè)空操作。
section:一個(gè)字符串,指定要為應(yīng)用的哪些部分創(chuàng)建 debuglog 函數(shù)。

demo:

const util = require('util');
const debuglog = util.debuglog('foo');
debuglog('hello from foo [%d]', 123);

util.deprecate(function, string)

說(shuō)明:

該方法會(huì)包裝給定的 function 或類,并標(biāo)記為廢棄的。

demo:

const util = require('util');
function isBoolean(obj){
 return (obj === true || obj === false);
}
isBoolean = util.deprecate(isBoolean, 'isBoolean 方法已被廢棄');
isBoolean(true);
//輸出:(node:9911) DeprecationWarning: isBoolean 方法已被廢棄

util.format(format[, …args])

說(shuō)明:

util.format() 方法返回一個(gè)格式化后的字符串,
format:第一個(gè)參數(shù)是一個(gè)字符串,包含零個(gè)或多個(gè)占位符。
每個(gè)占位符會(huì)被對(duì)應(yīng)參數(shù)轉(zhuǎn)換后的值所替換。 支持的占位符有:
%s:字符串
%d:數(shù)值(整數(shù)或浮點(diǎn)數(shù))
%i:整數(shù)
%f:浮點(diǎn)數(shù)
%j – JSON
%o – Object(包括不可枚舉的屬性方法)
%O – Object(不包括不可枚舉屬性)
%% – 單個(gè)百分號(hào)('%')不消耗參數(shù)。

demo:

const util = require('util');
var formatString = util.format('%s %d %i %f %j', 'hello', 1.123, 123, 2.1, "{'name': 'xiao', 'age': '18'}");
console.log(formatString);
//e輸出: hello 1.123 123 2.1 "{'name': 'xiao', 'age': '18'}"
//如果占位符沒(méi)有對(duì)應(yīng)的參數(shù),則占位符不被替換。
formatString = util.format('%s , %s', 'hello');
console.log(formatString);
//輸出: hello , %s
//如果傳入的參數(shù)比占位符的數(shù)量多,則多出的參數(shù)會(huì)被強(qiáng)制轉(zhuǎn)換為字符串,
//然后拼接到返回的字符串,參數(shù)之間用一個(gè)空格分隔。
formatString = util.format('%s , %s', 'hello', 'isjs', '!');
console.log(formatString);
//輸出: hello , isjs !
//如果第一個(gè)參數(shù)不是一個(gè)字符串,則返回一個(gè)所有參數(shù)用空格分隔并連在一起的字符串
formatString = util.format(1, 2, 3);
console.log(formatString);
//輸出: 1 2 3
//如果只傳入占位符而不傳入?yún)?shù),則原樣返回
formatString = util.format('%% , %s');
console.log(formatString);
//輸出: %% , %s

util.inherits(constructor, superConstructor)

說(shuō)明:

注意,不建議使用 util.inherits()。 請(qǐng)使用 ES6 的 class 和 extends 關(guān)鍵詞獲得語(yǔ)言層面的繼承支持。
從一個(gè)構(gòu)造函數(shù)中繼承原型方法到另一個(gè)。
constructor 的 prototype 會(huì)被設(shè)置到一個(gè)從 superConstructor 創(chuàng)建的新對(duì)象上。
superConstructor 可通過(guò) constructor.super_ 屬性訪問(wèn)

demo:

const util = require('util');
const EventEmitter = require('events');
function MyStream() { 
 EventEmitter.call(this);
}
util.inherits(MyStream, EventEmitter);
MyStream.prototype.write = function(data) {
 this.emit('data', data);
};
const stream = new MyStream();
console.log(stream instanceof EventEmitter); // true
console.log(MyStream.super_ === EventEmitter); // true
stream.on('data', (data) => {
 console.log(`接收的數(shù)據(jù):"${data}"`);
});
stream.write('運(yùn)作良好!'); // 接收的數(shù)據(jù):"運(yùn)作良好!"
//建議使用 ES6 的 class 和 extends:
const EventEmitter = require('events');
class MyStream extends EventEmitter {
 write(data) {
 this.emit('data', data);
 }
}
const stream = new MyStream();
stream.on('data', (data) => {
 console.log(`接收的數(shù)據(jù):"${data}"`);
});
stream.write('使用 ES6');

util.inspect(object[, options])

說(shuō)明:

方法返回 object 的字符串表示,主要用于調(diào)試。
object: 任何 JavaScript 原始值或?qū)ο?br /> options: 可用于改變格式化字符串的某些方面。

demo:

const util = require('util');
const inspectOpt = {
 showHidden: false,//如果為 true,則 object 的不可枚舉的符號(hào)與屬性也會(huì)被包括在格式化后的結(jié)果中。
 depth: 2,//指定格式化 object 時(shí)遞歸的次數(shù)。 默認(rèn)為 2。 若要無(wú)限地遞歸則傳入 null。
 colors: false,//如果為 true,則輸出樣式使用 ANSI 顏色代碼。 默認(rèn)為 false。
 customInspect: true,//如果為 false,則 object 上自定義的 inspect(depth, opts) 函數(shù)不會(huì)被調(diào)用。 默認(rèn)為 true
 showProxy: false,//如果為 true,則 Proxy 對(duì)象的對(duì)象和函數(shù)會(huì)展示它們的 target 和 handler 對(duì)象。 默認(rèn)為 false
 maxArrayLength: 100,//指定格式化時(shí)數(shù)組和 TypedArray 元素能包含的最大數(shù)量。 默認(rèn)為 100。 設(shè)為 null 則顯式全部數(shù)組元素。 設(shè)為 0 或負(fù)數(shù)則不顯式數(shù)組元素。
 breakLength: 60//一個(gè)對(duì)象的鍵被拆分成多行的長(zhǎng)度。 設(shè)為 Infinity 則格式化一個(gè)對(duì)象為單行。 默認(rèn)為 60。
};
console.log(util.inspect(util, inspectOpt));

util.inspect.styles, util.inspect.colors

說(shuō)明:

可以通過(guò) util.inspect.styles 和 util.inspect.colors 屬性全局地自定義 util.inspect 的顏色輸出(如果已啟用)。
預(yù)定義的顏色代碼有:white、grey、black、blue、cyan、green、magenta、red 和 yellow。
還有 bold、italic、underline 和 inverse 代碼。
顏色樣式使用 ANSI 控制碼,可能不是所有終端都支持。

demo:

const util = require('util');
console.log(util.inspect.styles);
// { special: 'cyan',
// number: 'yellow',
// boolean: 'yellow',
// undefined: 'grey',
// null: 'bold',
// string: 'green',
// symbol: 'green',
// date: 'magenta',
// regexp: 'red' }
console.log(util.inspect.colors);
// { bold: [ 1, 22 ],
// italic: [ 3, 23 ],
// underline: [ 4, 24 ],
// inverse: [ 7, 27 ],
// white: [ 37, 39 ],
// grey: [ 90, 39 ],
// black: [ 30, 39 ],
// blue: [ 34, 39 ],
// cyan: [ 36, 39 ],
// green: [ 32, 39 ],
// magenta: [ 35, 39 ],
// red: [ 31, 39 ],
// yellow: [ 33, 39 ] }

util.inspect.custom

說(shuō)明:

util.inspect.custom是一個(gè)符號(hào),可被用于聲明自定義的查看函數(shù):[util.inspect.custom](depth, opts)
自定義 inspect 方法的返回值可以使任何類型的值,它會(huì)被 util.inspect() 格式化。

demo:

const util = require('util');
class Box {
 [util.inspect.custom](depth, options) {
 return "myInspect";
 }
}
const box = new Box();
console.log(util.inspect(box));
// 輸出:myInspect
 

util.inspect.defaultOptions

說(shuō)明:

defaultOptions 值允許對(duì) util.inspect 使用的默認(rèn)選項(xiàng)進(jìn)行自定義。
它需被設(shè)為一個(gè)對(duì)象,包含一個(gè)或多個(gè)有效的 util.inspect() 選項(xiàng)。 也支持直接設(shè)置選項(xiàng)的屬性。

demo:

const util = require('util');
util.inspect.defaultOptions = {
 showHidden: true,
 depth:3
};
util.inspect.defaultOptions.breakLength = 30;
console.log(util.inspect.defaultOptions);
// { showHidden: true,
// depth: 3,
// colors: false,
// customInspect: true,
// showProxy: false,
// maxArrayLength: 100,
// breakLength: 30 }

util.promisify(original)

說(shuō)明:

讓一個(gè)遵循通常的 Node.js error first回調(diào)風(fēng)格的函數(shù),回調(diào)函數(shù)是最后一個(gè)參數(shù), 返回一個(gè)返回值是一個(gè) promise 版本的函數(shù)。

demo:

const util = require('util');
const fs = require('fs');
const stat = util.promisify(fs.stat);
stat('.').then((stats) => {
 // Do something with `stats`
}).catch((error) => {
 // Handle the error.
});

util.promisify.custom

說(shuō)明:

使用util.promisify.custom符號(hào)可以自定義promisified功能。

demo:

const util = require('util');
function doSomething(foo, callback) {
 // ...
}
doSomething[util.promisify.custom] = function(foo) {
 return getPromiseSomehow();
};
const promisified = util.promisify(doSomething);
console.log(promisified === doSomething[util.promisify.custom]);
// 輸出: true

類:util.TextEncoder

說(shuō)明:

該類用來(lái)對(duì)文本進(jìn)行編碼

textEncoder.encode([input])

說(shuō)明:

對(duì)input字符串進(jìn)行編碼并返回一個(gè)Uint8Array包含編碼字節(jié)的字符串

textEncoder.encoding

說(shuō)明:

TextEncoder實(shí)例支持的編碼。總是設(shè)置為'utf-8'。

demo:

const encoder = new TextEncoder();
const uint8array = encoder.encode('this is some data');
console.log(encoder.encoding)
//utf-8

類:util.TextDecoder

說(shuō)明:

該類用來(lái)解析編碼后的文本

new TextDecoder([encoding[, options]])

說(shuō)明:

創(chuàng)建一個(gè)TextDecoder實(shí)例。
encoding: 編碼方式,默認(rèn)'utf-8′
options: 選項(xiàng)
fatal: 解碼發(fā)生的錯(cuò)誤將導(dǎo)致 TypeError被拋出。默認(rèn)為 false
ignoreBOM: 解碼結(jié)果中是否會(huì)包含字節(jié)順序標(biāo)記。默認(rèn)為false。僅當(dāng)encoding的值為'utf-8','utf-16be'或'utf-16le'時(shí)有效。

textDecoder.decode([input[, options]])

說(shuō)明:

解碼input并返回一個(gè)字符串。
input: 待解碼數(shù)據(jù)
options.stream: 如果需要額外的數(shù)據(jù)塊,設(shè)置為true。默認(rèn)為false。

textDecoder.encoding

說(shuō)明:

返回textDecoder實(shí)例支持的編碼。

textDecoder.fatal

說(shuō)明:

返回textDecoder實(shí)例的fatal屬性,

textDecoder.ignoreBOM

說(shuō)明:

返回解碼結(jié)果是否包含字節(jié)順序標(biāo)記

希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • node.js抓取并分析網(wǎng)頁(yè)內(nèi)容有無(wú)特殊內(nèi)容的js文件

    node.js抓取并分析網(wǎng)頁(yè)內(nèi)容有無(wú)特殊內(nèi)容的js文件

    nodejs獲取網(wǎng)頁(yè)內(nèi)容綁定data事件,獲取到的數(shù)據(jù)會(huì)分幾次相應(yīng),如果想全局內(nèi)容匹配,需要等待請(qǐng)求結(jié)束,在end結(jié)束事件里把累積起來(lái)的全局?jǐn)?shù)據(jù)進(jìn)行操作,本文給大家介紹node.js抓取并分析網(wǎng)頁(yè)內(nèi)容有無(wú)特殊內(nèi)容的js文件,需要的朋友參考下
    2015-11-11
  • Nodejs 模塊化實(shí)現(xiàn)示例深入探究

    Nodejs 模塊化實(shí)現(xiàn)示例深入探究

    這篇文章主要為大家介紹了Nodejs 模塊化實(shí)現(xiàn)示例深入探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Centos7 中安裝 Node.js v4.4.4

    Centos7 中安裝 Node.js v4.4.4

    我一直對(duì)學(xué)習(xí)Node.js比較感興趣。這是一個(gè)Java平臺(tái)的服務(wù)器端編程 ,它允許開(kāi)發(fā)人員在服務(wù)器編寫Java代碼,并且有許多CentOS的用戶正努力學(xué)習(xí)這個(gè)語(yǔ)言的開(kāi)發(fā)環(huán)境。這正是我想做這個(gè)教程的目的。
    2016-11-11
  • Node接收電子郵件的實(shí)例代碼

    Node接收電子郵件的實(shí)例代碼

    本篇文章主要介紹了Node接收電子郵件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • nodejs版本管理工具nvm的安裝與使用小結(jié)

    nodejs版本管理工具nvm的安裝與使用小結(jié)

    在項(xiàng)目開(kāi)發(fā)過(guò)程中,使用到vue框架技術(shù),需要安裝node下載項(xiàng)目依賴,本文主要介紹了nodejs版本管理工具nvm的安裝與使用小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Node.js對(duì)MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)模糊查詢的方法

    Node.js對(duì)MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)模糊查詢的方法

    模糊查詢是數(shù)據(jù)庫(kù)的基本操作之一,下面這篇文章主要給大家介紹了利用Node.js對(duì)MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)模糊查詢的方法教程,文中給出了詳細(xì)的介紹和示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • nodeJS進(jìn)程管理器pm2的使用

    nodeJS進(jìn)程管理器pm2的使用

    這篇文章主要介紹了nodeJS進(jìn)程管理器pm2的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • node.js操作mongodb學(xué)習(xí)小結(jié)

    node.js操作mongodb學(xué)習(xí)小結(jié)

    這篇文章主要介紹了node.js操作mongodb學(xué)習(xí)小結(jié),本文給出了mongodb創(chuàng)建數(shù)據(jù)庫(kù)、插入數(shù)據(jù)以及連接mongodb數(shù)據(jù)庫(kù)并查詢數(shù)據(jù)等代碼實(shí)例,需要的朋友可以參考下
    2015-04-04
  • Node.js創(chuàng)建一個(gè)簡(jiǎn)單的服務(wù)器的實(shí)現(xiàn)

    Node.js創(chuàng)建一個(gè)簡(jiǎn)單的服務(wù)器的實(shí)現(xiàn)

    Node.js是一個(gè)基于Chrome V8引擎的JavaScript運(yùn)行時(shí)環(huán)境,可以在服務(wù)器端運(yùn)行JavaScript代碼,本文主要介紹了Node.js創(chuàng)建一個(gè)簡(jiǎn)單的服務(wù)器的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • Node.js API詳解之 dns模塊用法實(shí)例分析

    Node.js API詳解之 dns模塊用法實(shí)例分析

    這篇文章主要介紹了Node.js API詳解之 dns模塊用法,結(jié)合實(shí)例形式分析了Node.js API中dns模塊基本功能、相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下
    2020-05-05

最新評(píng)論