Skip to content

Instantly share code, notes, and snippets.

@DronRathore
Created August 21, 2015 14:52
Show Gist options
  • Save DronRathore/7ec6863841180305337a to your computer and use it in GitHub Desktop.
Save DronRathore/7ec6863841180305337a to your computer and use it in GitHub Desktop.
Generate Unique Object IDs
/*
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())
@DronRathore
Copy link
Author

Works both on nodejs as well as browsers.

@jdkanani
Copy link

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