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 '); 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); VarObjbase= New Base();Objbaseshowname (); Objbase. Console.objbasevar objsub = new sub (); Objsub.//objsub.sayhello (); console.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 Util.inherits implementation. The results of the operation are as follows:
base hello base { Name: , base: 1991, Sayhello: [function] }sub { Name: ' 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. If we remove Objsub.sayhello (); This line of comments, you will see:
Node.Js:201 ThrowE; Process.nexttick error, or ' Error ' event on first tick^ TypeError: Object #< Sub> Has no method ' SayHello 'AtObject.≪Anonymous≫ (/home/Byvoid/Utilinherits.Js:29:8)AtModule._compile(Module.Js:441:26)AtObject..Js(Module.Js:459:10)AtModule.Load(Module.Js:348:31)AtFunction._load (module. Js:308:12) at array. 0 (module. Js:479:10) at eventemitter. (node. Js:192:40)
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.
VarUtil= Require(' Util '); function Person() { This.Name= ' Byvoid '; This.Tostring= function() { Return This.Name; }; } var obj = new person (); Console. (util. Inspectobj console log (util. Inspect (obj, true< Span class= "pun"))
The operating result 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, 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 is returned.
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
For more details, please visit http://nodejs.org/api/util.html for more information.
node. JS Common Tools