Return to Snippet

Revision: 14934
at August 12, 2009 11:15 by inkdeep


Updated Code
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

*/

Revision: 14933
at June 17, 2009 15:44 by inkdeep


Initial Code
function put(message) { try{ console.log(message); }catch(e){ alert(message); } };
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

*/

Initial URL

                                

Initial Description
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.

Initial Title
Console Helper for Firebug

Initial Tags
javascript

Initial Language
JavaScript