Latest web development tutorials

Node.js common tools

util Node.js is a core module that provides a collection of commonly used functions, used to make up the core JavaScript features streamlined too inadequate.


util.inherits (constructor, superConstructor) is an implementation of a function prototype inheritance between objects.

Object-oriented features of JavaScript is a prototype-based, unlike the common class-based. JavaScript does not provide language-level objects inherit properties, but by copying the prototype to achieve.

Here we only introduce util.inherits usage examples are as follows:

var util = require('util'); 
function Base() { = 'base'; 
	this.base = 1991; 
	this.sayHello = function() { 
	console.log('Hello ' +; 
Base.prototype.showName = function() { 
function Sub() { = 'sub'; 
util.inherits(Sub, Base); 
var objBase = new Base(); 
var objSub = new Sub(); 

We define a base object inherits from Base and Base of a Sub, Base functions have three attributes defined within the constructor and a prototype defined by inheritance util.inherits. Results are as follows:

Hello base 
{ name: 'base', base: 1991, sayHello: [Function] } 
{ name: 'sub' }

Note: Sub Base only inherited the functions defined in the prototype, and the internal structure and function to create the base attribute sayHello function have not been Sub inherited.

Meanwhile, the properties in the prototype can not be defined as an object of property console.log output. If we remove objSub.sayHello (); Comment this line, you will see:

throw e; // process.nextTick error, or 'error' event on first tick 
TypeError: Object #<Sub> has no method 'sayHello' 
at Object.<anonymous> (/home/byvoid/utilinherits.js:29:8) 
at Module._compile (module.js:441:26) 
at Object..js (module.js:459:10) 
at Module.load (module.js:348:31) 
at Function._load (module.js:308:12) 
at Array.0 (module.js:479:10) 
at EventEmitter._tickCallback (node.js:192:40) 


util.inspect (object, [showHidden], [depth], [colors]) is an arbitrary object to a string method, typically used for debugging and error output. It accepts an object at least one parameter object, that is, to convert.

showHidden is an optional parameter, if true, the output will be more hidden information.

Maximum recursion depth indicates the number of layers, if the object is complex, you can specify how many layers to control the output of information. If you do not specify a depth, recursive default layer 2, shows a limited specified as null recursively traverse the object layers intact. If the color is true, the output format will be in ANSI color coding, it is generally used for a more beautiful effect on the terminal.

Of particular note is that, util.inspect not simply directly to the object to a string, even if the object defines the toString method is also not called.

var util = require('util'); 
function Person() { = 'byvoid'; 
	this.toString = function() { 
var obj = new Person(); 
console.log(util.inspect(obj, true)); 

Operating results are:

{ name: 'byvoid', toString: [Function] } 
{ toString: 
{ [Function] 
[prototype]: { [constructor]: [Circular] }, 
[caller]: null, 
[length]: 0, 
[name]: '', 
[arguments]: null }, 
name: 'byvoid' } 

util.isArray (object)

If the given parameter "object" is an array return true, otherwise it returns false.

var util = require('util');

  // true
util.isArray(new Array)
  // true
  // false

util.isRegExp (object)

If the given parameter "object" is a regular expression returns true, otherwise returns false.

var util = require('util');

util.isRegExp(/some regexp/)
  // true
util.isRegExp(new RegExp('another regexp'))
  // true
  // false

util.isDate (object)

If the given parameter "object" is a date returns true, otherwise returns false.

var util = require('util');

util.isDate(new Date())
  // true
  // false (without 'new' returns a String)
  // false

util.isError (object)

If the given parameter "object" is an error object returns true, otherwise returns false.

var util = require('util');

util.isError(new Error())
  // true
util.isError(new TypeError())
  // true
util.isError({ name: 'Error', message: 'an error occurred' })
  // false

More details can be accessed for details.