Unique Object Identifier in Javascript, Like Python Id
Fleeting- External reference: https://stackoverflow.com/questions/1997661/unique-object-identifier-in-javascript
It is a clever hack that adds the id property that stores some incremented value everytime it finds a new object.
(function() { if ( typeof Object.id != “undefined” ) return;
var id = 0;
Object.id = function(o) { if ( typeof o.__uniqueid != “undefined” ) { return o.__uniqueid; }
Object.defineProperty(o, “__uniqueid”, { value: ++id, enumerable: false, / This could go either way, depending on your / interpretation of what an “id” is writable: false });
return o.__uniqueid; }; })();
var obj = { a: 1, b: 1 };
console.log(Object.id(obj)); console.log(Object.id([])); console.log(Object.id({})); console.log(Object.id(.)); console.log(Object.id(function() {}));
for (var k in obj) { if (obj.hasOwnProperty(k)) { console.log(k); } } // Logged keys are `a` and `b`
— https://stackoverflow.com/questions/1997661/unique-object-identifier-in-javascript