When debugging complicated JavaScript one thing I find myself constantly
doing is using console.log()
to print
out what functions are being called in what order. JavaScript is
single-threaded and event driven so it's often not entirely clear what
functions will be called in what order.
Traditionally I've done something like this:
function foo(bar) { console.log('foo('+bar+')'); }but last night I came up with something better. It's probably not completely portable but it seems to work fine in recent Chrome / Safari / Firefox, which is really all I'm going to be using for debugging anyway:
function logCall() { console.log(logCall.caller.name + '(' + Array.prototype.slice.call(logCall.caller.arguments) .map(JSON.stringify).join(', ') + ')'); }Just add
logCall()
to the start of
functions and the function call (including serialized arguments) will be
logged to the console. Easy and foolproof.