Console Helper for Firebug


/ Published in: JavaScript
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. var put = function() { try{ console.log.apply(this,arguments); }catch(e){ alert(arguments); } };
  2. function logAllMembers( obj ) {
  3. for( var member in obj )
  4. if ( obj[member] != null)
  5. put(member + ' = ' + obj[member] + '\n Argument(s) length: ' + obj[member].length +'\n');
  6. };
  7.  
  8. /* use 'put' like this: put('this is a message that will appear in the console');
  9.  
  10. use 'logAllMembers' like this:
  11. // make a class
  12. FOO.insides = function (){};
  13. FOO.insides.prototype = {
  14.  
  15.   count: 10,
  16.   stub: [],
  17.  
  18.   methodOne: function(args){
  19.   return 'method one';
  20.   },
  21.  
  22.   methodTwo: function(args, args2, args3){
  23.   return 'method two'
  24.   }
  25. };
  26. // create a new instance
  27. bar = new FOO.insides();
  28.  
  29. bar.stub = ['apple','banana','pear','peach','apple'];
  30.  
  31. // look inside the instance
  32. logAllMembers(bar);
  33.  
  34. // will return this:
  35. stub = apple,banana,pear,peach,apple
  36.  Argument(s) length: 5
  37. count = 10
  38.  Argument(s) length: undefined
  39. methodOne = function (args) {
  40.   return "method one";
  41. }
  42.  Argument(s) length: 1
  43. methodTwo = function (args, args2, args3) {
  44.  return "method two";
  45. }
  46.  Argument(s) length: 3
  47.  
  48. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.