Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong> My original answer below was written 6 years ago in a style befitting the times and my understanding. In response to some conversation in the comments, a more modern approach to this is as follows:</p> <pre><code>(function() { if ( typeof Object.id == "undefined" ) { var id = 0; Object.id = function(o) { if ( typeof o.__uniqueid == "undefined" ) { 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` </code></pre> <p>If you have archaic browser requirements, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Browser_compatibility" rel="noreferrer">check here</a> for browser compatibility for <code>Object.defineProperty</code>.</p> <p>The original answer is kept below (instead of just in the change history) because I think the comparison is valuable. </p> <hr> <p>You can give the following a spin. This also gives you the option to explicitly set an object's ID in its constructor or elsewhere.</p> <pre><code>(function() { if ( typeof Object.prototype.uniqueId == "undefined" ) { var id = 0; Object.prototype.uniqueId = function() { if ( typeof this.__uniqueid == "undefined" ) { this.__uniqueid = ++id; } return this.__uniqueid; }; } })(); var obj1 = {}; var obj2 = new Object(); console.log(obj1.uniqueId()); console.log(obj2.uniqueId()); console.log([].uniqueId()); console.log({}.uniqueId()); console.log(/./.uniqueId()); console.log((function() {}).uniqueId()); </code></pre> <p>Take care to make sure that whatever member you use to internally store the unique ID doesn't collide with another automatically created member name.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload