I am working on a Logging system, and am looking forward to implement a log once method.
What I'm stuck with, is, how would I retrieve unique identifier and/or objects signature, to be used as a hash key?
My idea is:
var Log = {
messages : {},
once : function(message)
{
// get unique id and/or passed objects signature, use it as a key
var key = unique( message );
if ( !Log.messages.hasOwnProperty(key) )
{
Log.messages[key] = message;
}
}
};
I tried .toString()
, but it did return simply [object Object]
for a basic object, and [object <type>]
for anything else. No ids, no nothing.
I also tried the toSource()
function, but in Chrome it did not want to work with basic objects.
What I need, though, is for this to work with every type of object. Be it string, be it integer, be it function, I need to get that signature / id.
Maybe there is an implemention of such function somewhere around already?
Update
This is meant to do logging in a loop, but instead of logging on each iteration, log only once in order to prevent console pollution.
My particular loop is actually a game loop, and I am looking to debug some info.
Like:
for (var i = 0; i < 100; i++)
{
console.log('message');
}
// this would log 100 entries
Where:
for (var i = 0; i < 100; i++)
{
Log.once('message');
}
// would execute behind the scenes console.log() only once.