Created
August 21, 2015 14:52
-
-
Save DronRathore/7ec6863841180305337a to your computer and use it in GitHub Desktop.
Generate Unique Object IDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Generate unique IDs for Objects | |
*/ | |
( | |
function(ref){ | |
ref.__counting_tick_var = 0; | |
Object.prototype.id = function(){ | |
return ( | |
this.__counting_tick_var ? | |
this.__counting_tick_var : | |
(this.__counting_tick_var = ++__counting_tick_var) | |
) | |
} | |
} | |
)((typeof(global) == "undefined"? window: global)) | |
/* | |
How to? | |
*/ | |
var a = {}; | |
console.log(a.id()) |
It's unique when you are not storing it for future use.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works both on nodejs as well as browsers.