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 为模块类定义类变量或类 ...
随机推荐
- 完美高仿精仿京东商城手机客户端android版源码
完美高仿精仿京东商城手机客户端android版源码,是从安卓教程网那边转载过来的,这款应用源码非常不错的,也是一个非常优秀的应用源码的,希望能够帮到学习的朋友. _js_op> <igno ...
- C++中执行windows指令
执行windows指令: BOOL ExecDosCmd(]) { SECURITY_ATTRIBUTES sa; HANDLE hRead,hWrite; sa.nLength = sizeof(S ...
- CorelDRAW 二维码插件
随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...
- Spark小课堂Week3 FirstSparkApp(RDD开发)
Spark小课堂Week3 FirstSparkApp 问题:Java有哪些数据结构 大致有如下几种,其中List与Map是最重要的: List Map Set Array Heap Stack Qu ...
- 关于CSS的图像放大问题的解决,需要借助jQuery等直接用css3设置
W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- JS禁用浏览器退格键
我们在真实的项目开发中经常会使用JS 对键盘上的一些按键进行禁用,常见的比如说退格键(backspace/ 后退键),我在一个项目中就遇到过在页面编辑的时候禁用掉退格键,因为退格键会发生页面后退,代码 ...
- eclipse html插件的下载和安装
需求:需要在eclipse里面编辑html和jsp,语法高亮和语法提示,自动补全等. 1.下载GEF(依赖包): http://www.eclipse.org/downloads/download.p ...
- Timeline
Timeline面板 Chrome开发者工具详解(3)-Timeline面板 注: 这一篇主要讲解面板Timeline,参考了Google的相关文档,主要用于公司内部技术分享.. 更新时间:201 ...
- Linux:crontab命令学习
一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...
- 简单3d RPG游戏 之 004 攻击(一)
功能:实现点击键盘F键,怪物血量条减少,并且假定是近战,需要对距离进行判断,距离小于一定值的时候按F才会减少怪物的血条. 新建c#脚本PlayerAttack,绑定到Player,并在unity里将敌 ...