js debug console
03 October 2006 // javascript. things.
Debug Console is a small utility for printing debug messages on the "console". Console is simply a textarea hanging in the corner of the page. No popups, no fancy formatting, but simple, clean and cross-browser.
Console also provides utility functions for printing stack trace and for measuring execution speed.
Here's what the Console can do (feel free to click "try it" links, to hide the console reload the page).
p() -- print multiple arguments
p('hello', 99, ['a', 'b', 'c'], window.noSuchObject);
// whitespaces are "visible"
p('a line \n with tabs\t\t and spaces\t and tabs');
// trace nested array
ary = [1, 2, [33, [444, 555], 66], 7]
p(ary);
pp() -- print a single structured object
// second argument is a maximal depth (negative = infinite)
// by default, functions are not traced
var person = {
name : 'John', age : 33,
occupation : {
type : 'programmer',
skills : {
php : 'good',
js : 'expert',
ruby : 'poor'
},
points: [1, 2, [33, 44], 5]
},
someMethod: function() { alert('Hi') }
}
pp(person, -1);
// pp() is able to break infinite recursion
var ring = {
a: 1, b: 2, c: 3
}
ring.self = ring
pp(ring, -1);
// trace DOM node (default recursion depth 1 recommended)
pp(document.body);
dbgc "API"
Dbgc.print('Hello')
Dbgc.trace('Hello')
Dbgc.clear()
Dbgc.close()
alert(Dbgc.inspect('Hello'))
miscellaneous
// time()
// measure execution time
function foo() {
for(var i = 0; i != 500; i++)
Math.sin(i)
}
Dbgc.print(Dbgc.time(foo))
// stack()
// print stack trace
function a(x, y, z) {
Dbgc.stack();
}
function b(one, two) {
a(11, 22, 33);
}
function c() {
b('aa', 'bb');
}
c()
If you think this comment is spam or otherwise completely irrelevant here,
feel free to hide it.
The comment disappears immediately, though it is not deleted,
so I have an option to "unhide" it later.
comment on this