Nodejs Introductory Example one-tools (util)

Source: Internet
Author: User
Util
Util.format ()
Util.debug (String)
Util.log (String)
Util.inspect (object, Showhidden=false, depth=2)
Util.isarray (object)
Util.isregexp (object)
Util.isdate (object)
Util.iserror (object)
Util.pump (Readablestream, Writablestream, [callback])

Util.inherits (constructor, Superconstructor)


util

These functional functions are placed in the module ' util '. , use require (' util ') for access.
Util.format ()

Returns the string of the first parameter formatted, similar to printf.

The first argument is a string that contains a number or more other placeholders. Each placeholder is replaced with the corresponding parameter. The following placeholders are supported:
%s-string. %d-number (contains integer and float). %j-json. %%-a percent sign ('% '). This does not consume parameters.

If the placeholder does not have a corresponding argument, the placeholder will not be replaced.

Util.format ('%s:%s ', ' foo '); ' foo:%s '

If the number of arguments is more than the number of placeholders, the extra arguments are converted to strings using Util.inspect () and appended to the first argument string.


Util.format ('%s:%s ', ' foo ', ' Bar ', ' Baz '); ' Foo:bar Baz '


If you do not currently have a parameter to format. Returns a string that is a combination of all parameters, separated by a space in the middle of the combined string. Use Util.inspect () to convert to a string.

Util.format (1, 2, 3); ' 1 2 3 '
util.debug (String)

A synchronous output function. Procedures (process) and outputs (output) are blocked when standard errors (stderr) are encountered.

Require (' util '). Debug (' Message on stderr ');
Util.log (String)

Standard output with a timestamp.

Require (' util '). log (' timestamped message. ');
The output is as follows:
7 Dec 00:24:04-ss
Util.inspect (object, Showhidden=false, depth=2)

Returns the string form of an object, which is useful in debugging.

If ShowHidden is true, the object's non enumerated type properties are displayed.

If the value of the depth is provided, he tells inspect how many times to format the object recursively. It is useful to check for large and complex objects.
The default recursion is two times. Pass a null value to the Depath to make it infinitely recursive.


var util = require (' util ');

Console.log (Util.inspect (util, True, null));
Util.isarray (object)

Returns whether the given object is an array.

var util = require (' util ');

Util.isarray ([])
  //True
Util.isarray (new Array)
  //True
Util.isarray ({})
  //False
Util.isregexp (object)

Determines whether a given object is a regular expression.

var util = require (' util ');

Util.isregexp (/some regexp/)
  //True
util.isregexp (new RegExp (' another RegExp '))
  //True
Util.isregexp ({})
  //False
util.isdate (object)

Determines whether a given object is a date

var util = require (' util ');

Util.isdate (new Date ())
  //True
util.isdate (date ())
  //False (no ' new ' returns a String)
util.isdate ({ })
  //False
Util.iserror (object)

Determine if an object is an error

var util = require (' util ');

Util.iserror (New Error ())
  //True
Util.iserror (New TypeError ())
  //True
Util.iserror ({name: ' Error ') , message: ' An error occurred '})
  //False
util.pump (Readablestream, Writablestream, [callback])

Experiment

The data is read from Readablestream and sent to Writablestream, and when Writablestream.write (data) returns false Readablestream is paused until writablestream. Callback Drain error-handling function called.
Util.inherits (constructor, Superconstructor)

Inherits another native method from one of the constructors. When a new object is created, the original ecology of the constructor is set from the parent constructor (superclass).

As an additional convenience, the superclass is accessed through construction. Super_ property.

var util = require ("util");
var events = require ("events");

function MyStream () {
    events. Eventemitter.call (this);
}

Util.inherits (MyStream, events.) Eventemitter);

MyStream.prototype.write = function (data) {
    this.emit ("Data", data);
}

var stream = new MyStream ();

Console.log (Stream instanceof events. Eventemitter); True
console.log (mystream.super_ = = events. Eventemitter); True

stream.on ("Data", function (data) {
    console.log (' Received data: ' + data + ' ');
})
Stream.Write ("It works!"); Received data: "It works!"


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.