Util is the core module of Nodejs, providing a collection of commonly used functions that users make up for the lack of the functionality of the core JavaScript too thin
Util.inherits
is a function that implements the prototype inheritance between objects
The object-oriented nature of JavaScript is prototype-based and differs from common class- based ones. JavaScript does not provide the language-level attributes of object inheritance, but is implemented through prototype replication.
Example:
varUtil = require (' util '));functionFather () {//defined inside a constructor, cannot be inherited This. Name = ' base '; //defined inside a constructor, cannot be inherited This. Birth = 1991; //defined inside a constructor, cannot be inherited This. SayHello =function() {Console.log (' Hello ' + This. Name); }}//defined in the prototype, can be inheritedFather.prototype.age=18;//defined in the prototype, can be inheritedFather.prototype.showName =function() {Console.log ( This. Name); Console.log ( This. age);}//defined in the prototype, can be inheritedFather.prototype.showAge =function() {Console.log ( This. age);}functionSon () {}util.inherits (son,father);varObjbase =NewFather (); Objbase.showname (); Objbase.sayhello (); Console.log (objbase) ;varObjsub =NewSon (); Objsub.showage ();
We define a base object Father and a son,father that inherits from Father, defines two attributes (Name,birth) and a function (SayHello) within the constructor , and defines a property in the prototype ( Age) and two functions (showname,showage) to implement inheritance through Util.inherits.
Attention:
Son inherits only the functions defined by Father in the prototype , and the Father properties and SayHello functions created inside the constructor are not inherited by son.
Also, properties defined in the prototype are not output by Console.log as an object's property.
Util.inspect
Util.inspect (Object,[showhidden],[depth],[colors]) is a method that converts any object to a string, typically for debugging and error output. It accepts at least one parameter, object, which is to be converted.
ShowHidden is an optional parameter, and if the value is true, more hidden information will be output.
Depth represents the maximum number of recursive layers, and if the objects are complex, you can specify the number of layers to control how much output information is. If you do not specify depth, the default is 2 levels, which is specified as null to completely traverse the object with an unlimited number of recursive layers. If the color value is true, the output format will be encoded in ANSI color, typically used to display a more beautiful effect on the terminal.
In particular, Util.inspect does not simply convert an object to a string, even if the object defines the ToString method.
Example:
var util = require (' util 'function person ()} {this. Name = ' byvoid ' ; This function () { returnthis. Name; var New True
Results:
{name: ' byvoid 'null0'null
Util.isarray (object)
Returns true if the given argument "object" is an array, otherwise false is returned.
var=require(' util '); Util. IsArray([])//Trueutil. IsArray(newArray)//Trueutil. IsArray({})//False
Util.isregexp (object)
Returns true if the given argument "object" is a regular expression, otherwise false.
var util = require. Isregexp (/some regexp/) //trueutil. (new regexp ' another regexp ' //True util. //false
Util.isdate (object)
Returns true if the given argument "object" is a date, otherwise false is returned.
var util = require ( ' util ' util.new date ()) Span class= "PLN" > //trueutil. (date ()) // False (without ' new ' returns a String) util. //false
Util.iserror (object)
Returns true if the given argument "object" is an Error object, otherwise false is returned.
VarUtil= Require(' Util ');Util.IsError (new error ()) //trueutil. (new typeerror ()) //trueutil. ({ Name: ' Error ' }) //false
Nodejs Basics-Common tools util