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 - if true then the object's non-enumerable properties will be shown too. Defaults to false.为true时对象的非枚举类型属性将显示

  • depth - tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely pass null.

  • colors - if true, then the output will be styled with ANSI color codes. Defaults to false. Colors are customizable, see below.

  • customInspect - if false, then custom inspect() functions defined on the objects being inspected won't be called. Defaults to true.

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: whitegreyblackbluecyangreenmagentared and yellow. There are also bolditalicunderline 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 Arrayfalse 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 RegExpfalse 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 Datefalse 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!"
 

最新文章

  1. LeetCode-66-Plus One
  2. 使用JAVA直观感受快速排序与冒泡排序的性能差异
  3. IPC——匿名管道
  4. 用java写bp神经网络(三)
  5. POJ 2728 Desert King 最优比率生成树
  6. 基于回调的事件处理——重写onTouchEvent方法响应触摸屏事件
  7. 判断具有某个属性js、jQuery
  8. 【BZOJ2882】工艺(后缀数组)
  9. Android开发技巧——使用Dialog实现仿QQ的ActionSheet菜单
  10. Quartz Cron表达式详解
  11. Confluence 6 CSS 编辑技巧
  12. 微信小程序API 登录-wx.login(OBJECT) + 获取微信用户唯一标识openid | 小程序
  13. Java 读取某文件下的所有文件的大小 并将所有文件的文件名,以及对应大小输出在xls表格里
  14. 【jquery】$(document).ready() 与window.onload的区别
  15. 931. Minimum Falling Path Sum
  16. GridView练习题
  17. mac 使用svn记录
  18. [转]ubuntu 13.04 体验wine qq
  19. 如何在sublime中使用sftp
  20. Edgware Feign hystrix-dashboard

热门文章

  1. vs2008 c++工程如何设置生成调试信息
  2. ASP.NET的错误处理机制
  3. L012-oldboy-mysql-dba-lesson12
  4. 《编写高质量代码-Web前端开发修改之道》笔记--第二章 团队合作
  5. coreseek安装使用
  6. google map api 学习笔记
  7. Linux 启动直接进入 console,
  8. iOS 深复制&浅复制
  9. kissy使用注意事项
  10. linux编程之线性表