node.js ---- core module ---- common tools

util is a Node.js core module that provides a collection of commonly used functions to make up for the over-simplification of core JavaScript functions.


util.inherits

util.inherits(constructor, superConstructor) is a function that implements prototypal inheritance between objects.

The object-oriented nature of JavaScript is prototype-based, as opposed to the usual class-based ones. JavaScript does not provide the language-level feature of object inheritance, but instead does it through prototype copying.

Here we only introduce the usage of util.inherits, examples are 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 base object Base and a Sub that inherits from Base, Base has three properties defined in the constructor and a function defined in the prototype, and inheritance is achieved through util.inherits. The results are 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 neither the base property nor the sayHello function created inside the constructor is inherited by Sub.

Also, properties defined in the prototype will not be output by console.log as properties of the object. If we uncomment the line 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 to a string, usually used for debugging and error output. It accepts at least one parameter object, which is the object to convert.

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

depth indicates the maximum recursive level. If the object is complex, you can specify the number of levels to control how much information is output. If depth is not specified, it will recurse 2 levels by default. Specifying as null means that the object will be traversed completely without limit of recursive levels. If color is true, the output format will be color-coded in ANSI, usually used for prettier display on the terminal.

In particular, util.inspect does not simply convert an object to a string directly, even if the object defines the toString method.

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 result of running is:

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

util.isArray(object)

Returns true if the given argument "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 argument "object" is a regular expression, 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 argument "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.isError(object)

Returns true if the given argument "object" is an error object, false otherwise.

var util = require ('util');

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325658901&siteId=291194637