util.format(format[, …args])
In which the first argument may contain a placeholder or zero, following are the list of placeholders used.
console.dir()
, following are the optional options of utill.inspect()
function.
util.inherits(constructor, superConstructor)
The function contains 2 arguments in which the first argument should be the prototype function which is to be inherited to the object given in the second argument. A new object which is created by the superConstructor is set to the constructor’s prototype and this superConstructor can be accessed by using the following property.
construnctor.super_property
The snippet code below demonstrate simple example for working of util.inherit()
function, create a .js file with the code as shown below.
inherit.js
[c]
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);
console.log(MyStream.super_ === events.EventEmitter);
stream.on("data", function(data) {
console.log('Status: "' + data + '"');
})
stream.write("Recived data, process completed successfully");
[/c]
Compiling the above code will produce the output as shown in the image below.
util.isError(object)
and the value returns true if the argument is error and the value is false if the given argument is not an error, the functions allows only one argument.
The snippet code below demonstrate simple example for working of util.isError()
function, create a .js file with the code as shown below.
error.js
[c]
var util = require('util');
console.log(util.isError(new Error()));
console.log(util.isError(new TypeError()));
console.log(util.isError({ name: 'SPLessons', message: 'Online Tutorilas' }));
[/c]
Compiling the above code will produce the output as shown in the image below.
util.log(string)
The function allows only one argument and that argument is printed to the console. The snippet code below demonstrate the simple example which prints the given argument to the console.
log.js
[c]
var util = require('util');
util.log('Date and Time message.');
[/c]
Compiling the above code will produce the output as shown in the image below.
util.isDate(object) -
The function allows only one argument and returns true value if the given argument is a date if not then the value would be false. The snippet code below demonstrate the simple example which prints the true or false for the given argument to the console.
date.js
[c]
var util = require('util');
console.log(util.isDate(new Date()));
console.log(util.isDate(Date()));
console.log(util.isDate({}))
[/c]
Compiling the above code will produce the output as shown in the image below.
utill.print()
function and then writes them out to stdout
without adding a new line after each argument.
The snippet code below demonstrate the simple example which prints the given argument without adding a new line for each argument to the console.
print.js
[c]
var util = require("util");
util.print(1, 2, '3');
[/c]
Compiling the above code will produce the output as shown in the image below.
util.puts([...strings]) -
Multiple arguments are converted to strings using the utill.puts()
function and then writes them out to stderr
by adding a new line after each argument.
The snippet code below demonstrate the simple example which prints the given argument with adding a new line for each argument to the console.
puts.js
[c]
var util = require("util");
util.puts("1", "2","3");
[/c]
Compiling the above code will produce the output as shown in the image below.