node. JS Common Tools
Util is a node. JS Core module that provides a collection of commonly used functions to compensate for the lack of the functionality of core JavaScript too thinly.
Util.inherits
Util.inherits (constructor, Superconstructor) is a function that implements 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.
Here we only introduce the usage of util.inherits, with the following examples:
varUtil = require (' util ')); functionBase () { This. Name = ' base '; This. Base = 1991; This. SayHello =function() {Console.log (' Hello ' + This. Name); }; } Base.prototype.showName=function() {Console.log ( This. name);}; functionSub () { This. Name = ' Sub '; } Util.inherits (Sub, Base); varObjbase =NewBase (); Objbase.showname (); Objbase.sayhello (); Console.log (objbase) ;varObjsub =NewSub (); Objsub.showname ();//Objsub.sayhello ();Console.log (objsub);
We have defined a base object base and a sub,base inherited from base with three properties defined within the constructor and a function defined in a prototype, inherited through the util.inherits implementation. The results of the operation are as follows:
' Base ', base:1991' Sub '}
Note: The sub inherits only the functions defined by base in the prototype, and the base property and the SayHello function created inside the constructor are not inherited by Sub.
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.
var util = require (' util 'function person ()} {this. Name = ' byvoid ' ; This function () { returnthis. Name; var New True
The operating result is:
person {name: ' byvoid ', tostring: [function]}person { ' byvoid ', tostring: {[function] 0, ', null, null, [prototype]: {[constructor]: [Circular]}} }
Util.isarray (object)
Returns true if the given argument "object" is an array, otherwise false is returned.
Util.isregexp (object)
Returns true if the given argument "object" is a regular expression, otherwise false is returned.
Util.isdate (object)
Returns true if the given argument "object" is a date, otherwise false is returned.
Util.iserror (object)
Returns true if the given argument "object" is an Error object, otherwise false is returned.
node. JS Common Tools