美文网首页
Node util 工具模块 解析(详)

Node util 工具模块 解析(详)

作者: github加星点进来 | 来源:发表于2016-12-05 18:32 被阅读303次

本文基于 Node.js v7.2.0 文档解析 , 官方文档https://nodejs.org/api/util.html

UTIL模块的基本介绍

util模块,是一个Node.js核心模块,用于弥补核心JavaScript的一些功能过于精简的不足。并且还提供了一系列常用工具,用来对数据的输出和验证

util.debuglog(string) (用的少)

{string} - 被调试的程序节点部分 , 返回值: {Function} 日志处理函数

这个方法是在存在NODE_DEBUG环境变量的基础上,创建一个有条件写到stderr里的函数。
如果“节点” {string} 的名字出现在这个环境变量里,那么就返回一个功能类似于console.error()的函数.如果不是,那么返回一个空函数.

  const util = require('util');
  //NODE_DEBUG=mine node node-util.js
  const debuglog = util.debuglog('mine');
  debuglog('test from mine [%d]', 123)

代码是


debuglog

输出是


Paste_Image.png

util.format(format[, ...args]) 字符串格式化 【输出的时候实用】

format函数根据第一个参数,返回一个格式化字符串,支持的占位符有:"%s(字符串)"、"%d(数字<整型和浮点型>)"、"%j(JSON)"、"%%(转换成1个%字符串)"

let util = require('util')
let testJson = { title : "标题" }
let t1 = util.format('%s %d ----%j%%sass','nihao',991,testJson)
//nihao 991 ----{"title":"标题"}%sass
console.log(t1);

util.inspect(object[, options])

util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换为字符串的函数,通常用于调试和错误输出。它至少接受一个参数object,即要转换的对象

inspect.png

将object对象转换为字符串,
showHidden可选,是一个bool类型的参数,如果为true,则会输出更多的隐藏信息
depth可选,为最大的地柜层数,如果对象有很多层的话,需要对输出信息的层数进行限制,默认为两层,如果要地柜到底,用null就可以了
colors可选, 如果为true,输出格式就会以ANSI颜色编码,通常用于在终端显示更漂亮的效果
这个方法不会简单的吧对象转换为字符串形式,即使对象定义了tostring方法也不会调用

 //V7 以前的使用方法
 util.inspect(path,true,null,true)
 //V7 以后的使用方法,2者都行
 util.inspect(util, { showHidden: true, depth: null })

util.inherits(constructor, superConstructor) 【最实用】

继承 是一个实现对象间原型继承的函数

var util = require('util');
function Child() {
    this.name = 'Child';
    this.sayHello = function() {
        console.log('Hello ' + this.name);
    };
}
Child.prototype.showName = function() {
    console.log(this.name);
};
function Sub() {
    this.name = 'sub';
}
util.inherits(Sub, Child);
var objChild = new Child();
objChild.showName();
objChild.sayHello();
console.log(objChild);
var objSub = new Sub();
objSub.showName();
console.log(objSub);
inherits

过时的API 还有 (字面意思就看的懂)

depreacted

相关文章

  • Node util 工具模块 解析(详)

    本文基于 Node.js v7.2.0 文档解析 , 官方文档https://nodejs.org/api/uti...

  • 四、node(二)

    node模块 文件模块内置模块第三方模块 内置模块 util util.inherits(Child,Parent...

  • Node.js<六>

    Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScr...

  • node.js(十五)

    Node.js 常用工具util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScri...

  • Node 模块之 util URL queryString p

    第一部分 util util是一个Node.js核心模块,util模块设计的主要目的是为了满足Node内部API的...

  • 08_Node.js 工具模块 util

    util(工具)是一个 Node.js 核心模块,提供常用函数的集合,用于弥补核心 JavaScript 的功能 ...

  • node工具模块

    Node.js工具模块node工具模块分为OS,Path, Net, DNS, Domain模块 OS 字节顺序 ...

  • 那些年成为node攻城狮的路(二)

    模块加载机制浅析 node中模块分为核心模块和文件模块两大类,核心模块诸如fs、http、util...,文件模块...

  • Node.js Utilities

    Util 稳定性:2 - Stable util是用来支持Node.js内部APIs需求的模块,同时对于开发应用程...

  • querystring 与qs

    querystring是node内置模块,qs是npm模块 querystring的解析会忽略内置对象,qs的解析...

网友评论

      本文标题:Node util 工具模块 解析(详)

      本文链接:https://www.haomeiwen.com/subject/pvtlmttx.html