Super Powerful Debugging Function in Javascript

Super powerful debugging function in javascript. This function will output your variable in question to firebug console or html page.
With this function you can debug anything from Object, array, string, integer, etc. It is also recursive safe.


/**
 * @author: Ronald Pringadi, 2011
 * @desc: to debug a JavaScript Object
 * @usage: debug(yourObject);  
 * or debug(yourString); 
 * or debug(yourArray);  
 * @param object vTraceObject
 * @param string vPadding
 * @param object vPadding
 * 
 */

function debug(vTraceObject, vPadding, vPreviousTraceObject) {
	'use strict';

	function getObjectClass(vObj) {

		if (vObj && vObj.constructor && vObj.constructor.toString) {
			var arr = vObj.constructor.toString().match(/function\s*(\w+)/);
			if (arr && arr.length === 2) {
				return arr[1];
			}
		}
		return undefined;
	}
	function typeOf(vObj) {

		var vResult = typeof vObj;
		if (vResult === 'object') {
			try {
				if (vObj.length !== undefined) {
					vResult = 'array';
				}
			} catch (e) {
			}
		}

		return vResult;
	}	
	function print(vPadding, vType, vValue) {

		if (vPadding === undefined) {
			vPadding = '';
		}
		if (vType === undefined) {
			vType = '';
		}
		if (vValue === undefined) {
			vValue = '';
		}
		try{
			console.log(vPadding + ' (' + vType + '): ' + vValue + "\n");
		}catch(e){
			document.write(vPadding + ' (' + vType + '): ' + vValue + "\n");
		}
		
	}	

	if (vPadding === '' || vPadding === undefined) {
		vPadding = '';
	}
	if (typeOf(vTraceObject) === 'object'
			&& typeOf(vPreviousTraceObject) === 'object') {
		if (getObjectClass(vPreviousTraceObject) === getObjectClass(vTraceObject)) {
			print(vPadding, 'ERROR', 'RECURSION EXISTING ...');
			return null;
		}
	}

	var vType = typeOf(vTraceObject);
	if (vType === 'object' || vType === 'array') {
		if (vPadding === '') {
			print(vPadding, vType, '...');
			vPadding = '----';

		}
		try {
			var vTempType = '';
			var variable = '';
			for (variable in vTraceObject) {
				vTempType = typeOf(vTraceObject[variable]);
				if (vTempType === 'object' || vTempType === 'array') {
					print(vPadding, vTempType, variable);
					debug(vTraceObject[variable], vPadding + '----',
							vTraceObject);
				} else {
					print(vPadding, vTempType, variable + ' : '
							+ vTraceObject[variable]);
				}
			}
		} catch (e) {
			print(vPadding, 'ERR', 'EXCEPTION');
		}
	} else {
		print(vPadding, vType, vTraceObject);

	}


}

function objA() {
	this.varA1 = 'a1';
	this.varA2 = 'a2';
	this.myCars = new Array();
	this.myCars[0] = "Saab";
	this.myCars[1] = "Volvo";
	this.myCars[2] = "BMW";
}

function objB() {
	this.varB1 = 'b1';
	this.varB2 = 'b2';
	this.varB3 = new objA;

}
instanceB = new objB;

Output

(object): ...
---- (string): varB1 : b1
---- (string): varB2 : b2
---- (object): varB3
-------- (string): varA1 : a1
-------- (string): varA2 : a2
-------- (array): myCars
------------ (string): 0 : Saab
------------ (string): 1 : Volvo
------------ (string): 2 : BMW

Leave a Reply

Your email address will not be published. Required fields are marked *


nine + 6 =