node. JS Common Tools Util Package

Source: Internet
Author: User

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.iserror (obj);

Util.isdate (obj);

Util.inherits (Constr,super);

Util.isregexp (/some regexp/);

Util.isarray (obj);

Util.inspect (obj);

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:

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 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 Hello Base {name: ' base ', 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 throw E; Process.nexttick error, or ' Error ' event on first tick ^ Typeerror:object #<sub> have no method ' SayHello ' at OBJ Ect.<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

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.tostring = function () {     return this.name;     };} var obj = new person (); Console.log (Util.inspect (obj)); Consol E.log (Util.inspect (obj, true));

  

The operating result is:

person {name: ' Byvoid ', ToString: [function]}person {  name: ' byvoid ',  tostring:    {[function]     [length] : 0,     [name]: ",     [arguments]: null,     [caller]: null,     [prototype]: {[constructor]: [Circular]}}}

  

Util.isarray (object)

Returns true if the given argument "object" is an array, otherwise false is returned.

var util = require (' util '); Util.isarray ([])  //Trueutil.isarray (new Array)  //Trueutil.isarray ({})/  / False

  

Util.isregexp (object)

Returns true if the given argument "object" is a regular expression, otherwise false is returned.

var util = require (' util '), Util.isregexp (/some regexp/)  //Trueutil.isregexp (new RegExp (' another RegExp '))  // Trueutil.isregexp ({})  //False

  

Util.isdate (object)

Returns true if the given argument "object" is a date, otherwise false is returned.

var util = require (' util '); Util.isdate (new Date ())  //Trueutil.isdate (Date ())  //False (without ' new ' returns a S Tring) util.isdate ({})  //False

  

Util.iserror (object)

Returns true if the given argument "object" is an Error object, otherwise false is returned.

var util = require (' util '); Util.iserror (new Error ())  //Trueutil.iserror (New TypeError ())  //Trueutil.iserror ({name: ' Error ', message: ' An error occurred '})  False

  

Excerpt from: http://www.runoob.com/nodejs/nodejs-util.html

node. JS Common Tools Util Package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.