Tuesday, 2 October 2018

Node.js: console.dir(obj[, options])

console.dir(obj[, options])
Uses util.inspect() on obj and prints the resulting string to stdout. This function bypasses any custom inspect() function defined on obj.

Below table summarizes all the supported options of dir method.

Option
Description
Type
showHidden
If true then the object's non-enumerable and symbol properties will be shown too. Default: false.
boolean
depth
It tells util.inspect() how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. To make it recurse indefinitely, pass null. Default: 2.
number
colors
If true, then the output will be styled with ANSI color codes.
boolean

HelloWorld.js
class Foo {
  get [Symbol.toStringTag]() {
    return 'This will print Foo';
  }
}


console.dir(new Foo()); // 'Foo [This will print Foo] {}'


Output
Foo [This will print Foo] {}

If a class has instance properties, then those also printed by dir method.

HelloWorld.js
class Employee {
  constructor(firstName, lastName) {
    this.firstName = firstName;
 this.lastName = lastName;
  }
  
  get [Symbol.toStringTag]() {
    return this.firstName + " , " + this.lastName;
  }
}

console.dir(new Employee("Krishna", "Ponnam"));


Output
Employee [Krishna , Ponnam] { firstName: 'Krishna', lastName: 'Ponnam' }


Previous                                                 Next                                                 Home

No comments:

Post a Comment