node. JS (vi) UTIL module

Source: Internet
Author: User

basic usage of 1.inspect functions

Util.inspect (Object,[showhidden],[depth],[colors]) is a function 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, and we learn its simple usage. Use the following syntax:

    1. var util = require(' util ');
    2. var result = util. Inspect(object);
    3. Console. Log(result);
basic usage of 2.format functions

The Format function returns a formatted string based on the first argument, which is a string that can contain 0 or more placeholders. Each placeholder is replaced with its corresponding converted value, and the supported placeholders are: "%s (String)", "%d (numeric < integer and floating point >)", "%j (JSON)", "%" (a single percent of a semicolon is not a parameter).

1: If the placeholder does not have a corresponding parameter, the placeholder will not be replaced. As an example:

    1. var util = require(' util ');
    2. var result = util. Format('%s:%s ', ' foo ');
    3. Console. Log(result);
Operation Result:
    1. ' foo:%s '

2: If there are more than one parameter placeholder, the extra parameter will call Util.inspect () to the string. These strings are concatenated together and separated by a space. As an example:

    1. var util = require ( ' util '
    2. var result Span class= "pun" >= Util. ( '%s:%s ' , ' foo ' , ' bar ' , ' Baz '
    3. console.result
Operation Result:
    1. ' Foo:bar Baz '

3: If the first argument is a non-formatted string, all arguments are converted to a string and stitched together in a space, and the string is returned. As an example:

    1. var util = require(' util ');
    2. var result = util. Format(1, 2, 3);
    3. Console. Log(result);
Operation Result:
    1. ' 1 2 3 '
basic usage of 3.isArray functions

The IsArray function can determine whether an object is an array type, or it returns ture, otherwise false. The syntax is as follows:

    1. var util = require(' util ');
    2. var result = util. IsArray(object);
    3. Console. Log(result);
basic usage of 4.isDate functions

The IsDate function can determine whether the object is a date type, or return ture, otherwise false. The syntax is as follows:

    1. var util = require(' util ');
    2. var result = util. IsDate(object);
    3. Console. Log(result);
basic usage of 5.ISREGEXP functions

The ISREGEXP function can determine whether the object is a regular type, or return ture, otherwise false. The syntax is as follows:

    1. var util = require(' util ');
    2. var result = util. Isregexp(object);
    3. Console. Log(result);

node. JS (vi) UTIL module

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.