Getting the best from a console API
Despite it being not a part of JavaScript, we all use console API extensively to find out what is really happening during an app life cycle. The API, once introduced by the Firebug tool, is now available in every major JavaScript agent. Most developers just do simple logging using methods such as error, trace, log, and the decorator such as info and warn. Well, when we pass any values to console.log
, they are presented to the JavaScript Console panel. Usually, we pass a string describing a case and a list of various objects that we want to inspect. However, did you know that we can refer to these objects directly from the string in the manner of the PHP sprintf
? So the string given as the first argument can be a template that contains format specifiers for the rest of the arguments:
var node = document.body; console.log( "Element %s has %d child nodes; JavaScript object %O, DOM element %o", node.tagName, node.childNodes.length, node, node );
The...