Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Thursday, 6 November 2014

Node.js - How to dump a Javascript object as JSON (Javascript object inspection)

When debugging your Node.js application, sometimes you will find yourself wanting to inspect an entire object. The Javascript module utils can be extremely helpful in this task.

Imagine you want to inspect the object "myobject" created below

var myobject= new Object();
myobject.property1 = "some object property";
myobject.intproperty = 42;
myobject.child = new Object();
myobject.child.property2 = "a property of the child object";
myobject.onDoSomething = function(arguments){
   console.log("Say hi!");
}


You can dump "myobject" as JSON like this:
var util = require("util");
console.log( util.inspect(myobject) ) ;


utils.inspect also allows controlling what will be dumped and how deep inside the object tree to go. This is done using the arguments showHidden and depth as show below
console.log( util.inspect(myobject, { showHidden: true, depth: 0 }) ) ;


You'll see that only the first level of the object contents is displayed. And you will also see that hidden attributes/objects/properties are dumped. To do a deep inspection of all levels, set depth to null
console.log( util.inspect(myobject, { showHidden: true, depth: null }) ) ;

Friday, 31 October 2014

How to open an URL in a new window in Javascript

If you want to open a web page on another window using javascript, just use the method open of the object window.
Very useful to keep users on your web site
Example
Just add the line
  window.open("http://devhelpers.blogspot.ca");
Inside a <script> tag