JSFiddle - React, Tailwind, and code Playground

showObjectProperties() experiment

by Laurens Maneschijn

HTML

See js panel and console for demo of showObjectProperties() experiment.

JavaScript

function showObjectProperties(obj, options) {
	if (typeof obj !== 'object') {
		return;
	}
	var options_default = {show_get_methods: false};
	options = (typeof options === 'object') ? options : {};
	options = Object.assign({}, options_default, options);

	console.groupCollapsed('showObjectProperties: ' + obj.constructor.name);
	console.log(obj);
	console.log('.constructor.name:', obj.constructor.name);

	var property_result, v;
	var property_results = [];
	// TODO: use for..in with hasOwnProperty(), or use Object.getOwnPropertyNames() ?
//	for(p in obj) {
//		if (!obj.hasOwnProperty(p)) { continue; }
	Object.getOwnPropertyNames(obj.__proto__).forEach(function(p,i){
		v = obj[p];
		property_result = {
			property: p,
			value_type: v === null ? 'null' : typeof v,
			value: v
		};
		if (typeof v === 'object') {
			try {
				property_result.constructor_name = v.constructor.name;
			} catch (e) {}
		}
		if (options.show_get_methods && typeof v === 'function' && p.match(/^(get|to)[A-Z_]/) ) {
			// EXPERIMENTAL warning!
			// Calling functions could modify original object or do other things.
			// Usually methods like: getXxx() toXxx() get_xxx() to_xxx() 
			// only return a value and do not change other things,
			// but that is not a guarantee.
			try {
				// calling v() throws an error:
				// TypeError: Method Date.prototype.toString called on incompatible receiver undefined
				// property_result.f_value = v();
				// but calling obj[p](); is ok.
				property_result.f_value = obj[p]();
			} catch (e) {
				property_result.f_value_error = e;
			}
		}
		property_results.push(property_result);
//	}
	});
	console.table(property_results);
	console.groupEnd();
}

	showObjectProperties((new Date()), {show_get_methods:true});
	showObjectProperties(document.body, {show_get_methods:true});