Ok so let's say I have an assortment of varables:
tab = document.createOjbect('table');
str = 'asdf';
ary = [123, 456, 789];
obj = {a:123, b:456, c:789};
A Piece of code to 'stringify' them:
function Stringify(){
var con = this.constructor.name;
if(con == 'String') return this;
if(con == 'Arrray') return this.toString();
if(con == 'Object') return JSON.stringify(this);
if(con == 'HTMLTableElement') return this.outerHTML;
}
An array containing the variables:
var aVar = [tab, str, ary, obj];
And I loop the array to 'stringify' its content:
for(var i in aVar){
console.log(
Stringify.call( aVar[i] );
);
}
I get the expected list of stringed objects:
<table></table>
asdf
123,456,789
{"a":123,"b":456,"c":789}
But what if I wanted to include the name of the variables in the logs?:
tab: <table></table>
str: asdf
ary: 123,456,789
obj: {"a":123,"b":456,"c":789}
How would I even go about that?:
for(var i in aVar){
var id = // get the name of the variable at aVar[i]
console.log(
id + ': ',
Stringify.call( aVar[i] );
);
}