Node.js common tools
util is a Node.js core module that provides a collection of commonly used functions to make up for the shortcomings of core JavaScript functions that are too streamlined.
util.inherits
util.inherits(constructor, superConstructor) is a function that implements prototypal inheritance between objects.
The object-oriented features of JavaScript are prototype-based, which is different from the common class-based ones. JavaScript does not provide language-level features of object inheritance, but does so through prototype copying.
Here we only introduce the usage of util.inherits. The example is as follows:
var util = require('util'); function Base() { this.name = 'base'; this.base = 1991; this.sayHello = function() { console.log('Hello ' + this.name); }; } Base.prototype.showName = function() { console.log(this.name); }; function Sub() { this.name = 'sub'; } util.inherits(Sub, Base); var objBase = new Base(); objBase.showName(); objBase.sayHello(); console.log(objBase); var objSub = new Sub(); objSub.showName(); //objSub.sayHello(); console.log(objSub);
We define a basic object Base and a Sub inherited from Base. Base has three definitions in the constructor Properties and functions defined in a prototype are inherited through util.inherits. The running result is as follows:
base Hello base { name: 'base', base: 1991, sayHello: [Function] } sub { name: 'sub' }
Note: Sub only inherits the functions defined by Base in the prototype, and the base attribute and sayHello function created inside the constructor are not inherited by Sub.
At the same time, the properties defined in the prototype will not be output by console.log as properties of the object. If we remove the comment from objSub.sayHello();, we will see:
node.js:201 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
util.inspect(object,[showHidden],[depth ],[colors]) is a method that converts an arbitrary object into a string, often used for debugging and error output. It accepts at least one parameter object, which is the object to be converted.
showHidden is an optional parameter. If the value is true, more hidden information will be output.
depth represents the maximum number of recursion levels. If the object is complex, you can specify the number of levels to control the amount of output information. If depth is not specified, 2 levels of recursion will be performed by default. Specifying null means that the object will be completely traversed without limiting the number of recursion levels. If the color value is true, the output format will be ANSI color-coded, usually used to display more beautiful effects in the terminal.
It should be pointed out in particular that util.inspect will not simply convert the object into a string directly, even if the object defines the toString method, it will not be called.
var util = require('util'); function Person() { this.name = 'byvoid'; this.toString = function() { return this.name; }; } var obj = new Person(); console.log(util.inspect(obj)); console.log(util.inspect(obj, true));
The operation result is:
{ 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 return false .
var util = require('util'); util.isArray([]) // true util.isArray(new Array) // true util.isArray({}) // false
util.isRegExp(object)
Returns true if the given parameter "object" is a regular expression, otherwise returns false.
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 parameter "object" is a date, otherwise returns false.
var util = require('util'); util.isDate(new Date()) // true util.isDate(Date()) // false (without 'new' returns a String) util.isDate({}) // false
util.isError(object)
Returns true if the given parameter "object" is an error object, 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
For more details, please visit http://nodejs.org/api/util.html for details.