node.js模块之util模块
util提供了各种使用的工具。require('util')
to access them.
Util.format(format,[..])
Returns a formatted string using the first argument as a printf
-like format.
The first argument is a string that contains zero or more placeholders. Each placeholder is replaced with the converted value from its corresponding argument. Supported placeholders are:
%s
- String.%d
- Number (both integer and float).%j
- JSON.%
- single percent sign ('%'
). This does not consume an argument.
If the placeholder does not have a corresponding argument, the placeholder is not replaced.
util.format('%s:%s', 'foo'); // 'foo:%s'
If there are more arguments than placeholders, the extra arguments are converted to strings withutil.inspect()
and these strings are concatenated, delimited by a space.
util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
If the first argument is not a format string then util.format()
returns a string that is the concatenation of all its arguments separated by spaces. Each argument is converted to a string with util.inspect()
.
util.format(1, 2, 3); // '1 2 3'
util.log(string)
带有时间戳的标准输出。
require('util').log('Timestamped message.');
输出如下:
7 Dec 00:24:04 - ss
util.inspect(object, [options])#
Return a string representation of object
, which is useful for debugging.
An optional options object may be passed that alters certain aspects of the formatted string:
showHidden
- iftrue
then the object's non-enumerable properties will be shown too. Defaults tofalse
.为true时对象的非枚举类型属性将显示depth
- tellsinspect
how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to2
. To make it recurse indefinitely passnull
.colors
- iftrue
, then the output will be styled with ANSI color codes. Defaults tofalse
. Colors are customizable, see below.customInspect
- iffalse
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
.
Example of inspecting all properties of the util
object:
var util = require('util');
console.log(util.inspect(util, { showHidden: true, depth: null }));
Customizing util.inspect
colors#
Color output (if enabled) of util.inspect
is customizable globally via util.inspect.styles
andutil.inspect.colors
objects.
util.inspect.styles
is a map assigning each style a color from util.inspect.colors
. Highlighted styles and their default values are: number
(yellow) boolean
(yellow) string
(green) date
(magenta) regexp
(red) null
(bold) undefined
(grey) special
- only function at this time (cyan) * name
(intentionally no styling)
Predefined color codes are: white
, grey
, black
, blue
, cyan
, green
, magenta
, red
and yellow
. There are also bold
, italic
, underline
and inverse
codes.
Objects also may define their own inspect(depth)
function which util.inspect()
will invoke and use the result of when inspecting the object:
var util = require('util');
var obj = { name: 'nate' };
obj.inspect = function(depth) {
return '{' + this.name + '}';
};
util.inspect(obj);
// "{nate}"
util.isArray(object)#
Returns true
if the given "object" is an Array
. false
otherwise.
var util = require('util');
util.isArray([])
// true
util.isArray(new Array)
// true
util.isArray({})
// false
util.isRegExp(object)#
Returns true
if the given "object" is a RegExp
. false
otherwise.
var util = require('util');
util.isRegExp(/some regexp/)
// true
util.isRegExp(new RegExp('another regexp'))
// true
util.isRegExp({})
// false
util.isDate(object)#
Returns true
if the given "object" is a Date
. false
otherwise.
var util = require('util');
util.isDate(new Date())
// true
util.isDate(Date())
// false (without 'new' returns a String)
util.isDate({})
// false
下面这个很实用:
util.inherits(constructor, superConstructor)#
Inherit the prototype methods from one constructor into another. The prototype of constructor
will be set to a new object created from superConstructor
.
As an additional convenience, superConstructor
will be accessible through the constructor.super_
property.
var util = require("util");
var events = require("events"); function MyStream() {
events.EventEmitter.call(this);
} util.inherits(MyStream, events.EventEmitter); MyStream.prototype.write = function(data) {
this.emit("data", data);
} var stream = new MyStream(); console.log(stream instanceof events.EventEmitter); // true
console.log(MyStream.super_ === events.EventEmitter); // true stream.on("data", function(data) {
console.log('Received data: "' + data + '"');
})
stream.write("It works!"); // Received data: "It works!"
node.js模块之util模块的更多相关文章
- node.js(七) 子进程 child_process模块
众所周知node.js是基于单线程模型架构,这样的设计可以带来高效的CPU利用率,但是无法却利用多个核心的CPU,为了解决这个问题,node.js提供了child_process模块,通过多进程来实现 ...
- node.js第二天之模块
一.模块的定义 1.在Node.js中,以模块为单位划分所有功能,并且提供了一个完整的模块加载机制,这时的我们可以将应用程序划分为各个不同的部分. 2.狭义的说,每一个JavaScript文件都是一个 ...
- node.js中使用http模块创建服务器和客户端
node.js中的 http 模块提供了创建服务器和客户端的方法,http 全称是超文本传输协议,基于 tcp 之上,属于应用层协议. 一.创建http服务器 const http = require ...
- Node.js 常用工具 util
util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherits(constructor ...
- Node.js 常用工具util
util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherits(constructor ...
- Node.js 常用工具util包
Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.isError(obj); util.is ...
- 31.Node.js 常用工具 util
转自:http://www.runoob.com/nodejs/nodejs-module-system.html util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaS ...
- node.js 下使用 util.inherits 来实现继承
上一篇博客说到了node.js继承events类实现事件发射和事件绑定函数,其中我们实现了一个公用基类 _base ,然后在模型中差异化的定义了各种业务需要的模型并继承 _base 公共基类.但是其中 ...
- 使用Node.js的socket.io模块开发实时web程序
首发:个人博客,更新&纠错&回复 今天的思维漫游如下:从.net的windows程序开发,摸到nodejs的桌面程序开发,又熟悉了一下nodejs,对“异步”的理解有了上上周对操作系统 ...
- Node.js权威指南 (4) - 模块与npm包管理工具
4.1 核心模块与文件模块 / 574.2 从模块外部访问模块内的成员 / 58 4.2.1 使用exports对象 / 58 4.2.2 将模块定义为类 / 58 4.2.3 为模块类定义类变量或类 ...
随机推荐
- CSS3单位
em 做前端的应该对em不陌生,不是什么罕见的单位,是相对单位,参考物是父元素的font-size,具有继承的特点.如果字体大小是16px(浏览器的默认值),那么 1em = 16px. 不过,这样使 ...
- php微信支付(仅Jsapi支付)详细步骤.----仅适合第一次做微信开发的程序员
本人最近做了微信支付开发,是第一次接触.其中走了很多弯路,遇到的问题也很多.为了让和我一样的新人不再遇到类似的问题,我把我的开发步骤和问题写出来,以供参考. 开发时间是2016/8/2,所以微信支付的 ...
- Mindjet.MindManager“参数错误”解决办法,适用于9.0、10.0和14.0
MindManager出14.0版本了,但是在应用个别模板的时候会提示“参数错误”,然后自动关闭. 解决办法: 如果是win7系统,可以进入C:\Users\(用户名) \AppData\Loca ...
- json 语义分析
json 中:元素与值用冒号 ":" 隔开元素与元素用逗号 "," 隔开{} 之间是一个对象, 对象可以层层嵌套[] 表示数组, 数组元素用逗号 ", ...
- win10 安装scrapy
在win10的环境下安装scrapy,并不能直接按照官网的手册(http://doc.scrapy.org/en/1.0/intro/install.html)一次性安装成功,根据我自己的安装过程中遇 ...
- ffmpeg yuv转h264
ffmpeg -s 176x144 -i container_qcif_176_144.yuv -b:v 7776k -r 25 -vcodec libx264 ds.h264
- SQL*Loader使用详解(一)
1.SQL*Loader介绍 1)SQL*Loader是一个从外部文件指加载数据到Oracle数据库的工具.语法类似于DB2的Load语法,但SQL*Loader支持各种load格式.选择性load和 ...
- Hibernate从入门到精通(二)Hibernate实例演示
上篇Hibernate从入门到精通(一)JDBC简介,我们主要对JDBC进行了简单介绍和使用说明,这次我们做一个Hibernate简单实例,通过这个实例对比Hibernate和JDBC,了解Hiber ...
- Contest2037 - CSU Monthly 2013 Oct (Problem J: Scholarship)
http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=9 [题解]: 这题卡了一下,卡在负数的情况,负数输出 0 这题主要找到一 ...
- 第一次写python
这是一个在BJDP上学习Coding Kata的时候用到的一个练习,原来打算用Java写的,但是一想正好是学习的好机会. 就用Python了.第一次,写的有些复杂. 这个题目是关于购买图书的打折信息的 ...