Util is a node. JS Core module that provides a collection of commonly used functions to compensate for the lack of the core Javascript functionality too thin.
Util.inherits (constructor, Superconstructor)
A function that implements prototype inheritance between objects, and the first constructor inherits the method of the prototype from the second constructor
The object-oriented nature of JavaScript is prototype-based and differs from common class-based ones. JavaScript does not provide language-level attributes for object inheritance, but rather uses prototype replication to implement
The sample code is as follows:
var util = require (' util ');//Base Object function Base () {this.name = ' base '; this.base = ' 1990 '; This.sayhello = function () {console.log (' Hello ' +this.name); }}; Base.prototype.showName = function () {console.log (this.name);};/ /Inherit from Base object 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 (); 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 are as follows:
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/8C/B9/wKioL1h16b2wjkqZAAAlbaPiTAo783.png "title=" Qq20170111161541.png "alt=" Wkiol1h16b2wjkqzaaalbapitao783.png "/>
Note that the sub inherits only the functions defined by base in the prototype, and neither the base property created inside the constructor nor the SayHello function are inherited by Sub. Also, properties defined in the prototype are not output by Console.log as an object's property.
Try printing output Objsub.sayhello () This method, the result of the output is:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/8C/B9/wKioL1h16sfiVn4CAACwgju0zUY918.png "title=" Qq20170111161941.png "alt=" Wkiol1h16sfivn4caacwgju0zuy918.png "/>
Util.inspect (Object,[showhidden],[depth],[colors]
is a method of converting any object to a string, typically for debugging and error output.
Util.inspect accepts at least one parameter, object, which is to be converted.
Other optional parameters are as follows:
showHidden
<boolean> is an optional parameter, and if the value is true, more hidden information will be output
depth
<number> 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 specifydepth
, the default is 2 layers, which is specified as NULL, which means no limit on the number of recursive layers and the complete traversal of the object
colors
<boolean> If the value is true, the output format will be encoded in ANSI color, typically used to display more beautiful effects at the terminal
In particular, Util.inspect does not simply convert an object to a string, even if the object defines the ToString method and does not invoke the
The sample code is as follows:
var util = require (' util '); function person () {this.name = ' Roger '; this.tostring = function () {return this.name; }}var obj = new person (); Console.log (Util.inspect (obj)); Console.log (Util.inspect (obj, true));
Operation Result:
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/8C/BA/wKioL1h175qAEq-gAAAyNT_RZpk861.png "title=" Qq20170111164043.png "alt=" Wkiol1h175qaeq-gaaaynt_rzpk861.png "/>
This article is from the "Dapengtalk" blog, make sure to keep this source http://dapengtalk.blog.51cto.com/11549574/1891133
node. js Common Tools Util