Is there a unique identifier for an object in javascript?
Asked Answered
A

1

10

Is there a searchable, unique identifier (i.e., key, uid, etc... - pardon the likely butchering of proper terms) to differentiate between ob1 and ob2 in this example?

var makeObject = function() {
return {example: 'property'};
};

var ob1 = makeObject();
var ob2 = makeObject();

Now we know that:

ob1 === ob2;  // false

Beyond this boolean distinction of inequality, are there lower level unique characters being temporarily assigned to ob1 and ob2? If so, can these unique characters be accessed through the console?

EDIT: How to determine equality for two JavaScript objects? really gets into the differentiation aspect of my question, so thank you for that. I feel that the other part of my question focused more on the machine level difference of ob1 compared to ob2 (practicality and/or user accessibility aside) and I believe "different places in memory" certainly answers that.

Alcoholometer answered 7/2, 2017 at 0:20 Comment(3)
It returns false because the reference pointed by obj1 and obj2 are not the same space in memory (both points to a different object). {} === {} will be false too. The comparison doesn't care about properties.Miry
I believe this is a duplicate question: #201683 and #1069334Bellyband
Possible duplicate of Object comparison in JavaScriptMiry
C
7

are there lower level unique characters being temporarily assigned to ob1 and ob2

Short answer: no, nothing that you'd be able to access.

By the way, the reason obj1 and obj2 are not equal in your example is because they're actually pointing to different places in memory. That's the only comparison that's being done (this is different than, say, JavaScript string comparison, which does not compare memory locations, but the value of the string itself).

Conias answered 7/2, 2017 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.