Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js a common tool


May 10, 2021 Node.js


Table of contents


Node .js a common tool util

This section describes .js common tool util.

As a core module .js Node, util provides a collection of common functions to compensate for the overly streamlined functionality of the core JavaScript.


util.inherits

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

Unlike common class-based, JavaScript's object-oriented nature is prototype-based. JavaScript does not provide language-level attributes for object inheritance, but is implemented through prototype replication.

Here we will only introduce the use of util.inherits, 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 inherited from Base, which has three properties defined within the constructor and a function defined in a prototype, which are inherited through util.inherits. The results are as follows:

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

Note: Sub inherits only the functions defined by Base in the prototype, while neither the base properties created inside the constructor nor the SayHello functions are inherited by Sub.

At the same time, properties defined in the prototype are not .log output of objects. I f we remove objSub.sayHello (); Comments on this line 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

The util.inspect method can convert any object to a string, typically for debugging and error output. It accepts at least one object parameter, the object to be converted.

ShowHidden is an optional parameter that outputs more hidden information if the value is true.

Depth represents the maximum number of recursive layers, and if the object is complex, you can specify the number of layers to control how much information is output. I f you do not specify deepth, the default recursive layer 2 is specified as null, which indicates that the object will be fully traversed without limiting the number of recursive layers. If the color value is true, the output format will be color-coded in ANSI and is typically used to display more beautiful effects at the terminal.

In particular, util.inspect does not simply convert an object directly to a string, even if the object defines a 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 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 that returns true, otherwise it returns false.

var util = require('util');

util.isArray([])
  // true
util.isArray(new Array)
  // true
util.isArray({})
  // false

util.isRegExp(object)

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

var util = require('util');

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

util.isDate(object)

If the given parameter "object" is a date to return true, otherwise it 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)

If the given parameter "object" is an error object returns true, otherwise it 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 http://nodejs.org/api/util.html for more information.