/ Published in: JavaScript
This comes in handy when working in big objects and you need to see what's happening on the inside of an instance.
I aliased 'console.log' to 'put' and wrapped it in a try/catch block that throws alerts if no console is available. Less typing - and mildly rubyish.
I got the logAllMembers bit from a mailing list archived message - forget the author but props to them. Only alteration I made was to skip null members in the output.
I aliased 'console.log' to 'put' and wrapped it in a try/catch block that throws alerts if no console is available. Less typing - and mildly rubyish.
I got the logAllMembers bit from a mailing list archived message - forget the author but props to them. Only alteration I made was to skip null members in the output.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var put = function() { try{ console.log.apply(this,arguments); }catch(e){ alert(arguments); } }; function logAllMembers( obj ) { for( var member in obj ) if ( obj[member] != null) put(member + ' = ' + obj[member] + '\n Argument(s) length: ' + obj[member].length +'\n'); }; /* use 'put' like this: put('this is a message that will appear in the console'); use 'logAllMembers' like this: // make a class FOO.insides = function (){}; FOO.insides.prototype = { count: 10, stub: [], methodOne: function(args){ return 'method one'; }, methodTwo: function(args, args2, args3){ return 'method two' } }; // create a new instance bar = new FOO.insides(); bar.stub = ['apple','banana','pear','peach','apple']; // look inside the instance logAllMembers(bar); // will return this: stub = apple,banana,pear,peach,apple Argument(s) length: 5 count = 10 Argument(s) length: undefined methodOne = function (args) { return "method one"; } Argument(s) length: 1 methodTwo = function (args, args2, args3) { return "method two"; } Argument(s) length: 3 */