Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Object Identities

Tags:

javascript

Objects in JavaScript have unique identities. Every object you create via an expression such as a constructor or a literal is considered differently from every other object.

What is the reason behind this?

{}==={}//output:false

For what reason they are treated differently? What makes them different to each other?

like image 468
Maizere Pathak.Nepal Avatar asked Apr 27 '13 18:04

Maizere Pathak.Nepal


People also ask

Do JavaScript objects have an ID?

No, objects don't have a built in identifier, though you can add one by modifying the object prototype.

What is object ID in JS?

ObjectID() id (string) – Can be a 24 byte hex string, 12 byte binary string or a Number.

What is object identity example?

Object identity - Each object is uniquely identifiable. For example, the fridge can not become the T.V.


4 Answers

{} creates a new object.

When you try and compare two, separate new objects (references), they will never be equal.

Laying it out:

var a = {};  // New object, new reference in memory, stored in `a`
var b = {};  // New object, new reference in memory, stored in `b`

a === b;  // Compares (different) references in memory

If it helps, {} is a "shortcut" for new Object(), so more explicitly:

var a = new Object();
var b = new Object();

a === b;  // Still false

Maybe the explicitness of new helps you understand the comparison compares different objects.

On the other side, references can be equal, if they point to the same object. For example:

var a = {};
var b = a;

a === b;  // TRUE
like image 132
Ian Avatar answered Oct 05 '22 21:10

Ian


This question is quite old, but I think the actual solution does not pop out clearly enough in the given answers, so far.

For what reason they are treated differently? What makes them different to each other?

I understand your pain, many sources in the internet do not come straight to the fact:

Object (complex JS types => objects, arrays and functions) variables store only references (=address of the instances in memory) as their value. Object identity is recognized by reference identity.

You expected something like an ID or reference inside the object, which you could use to tell them apart (maybe that's actually done transparently, under the hood). But every time you instantiate an object, a new instance is created in memory and only the reference to it is stored in the variable.

So, when the description of the ===-operator says that it compares the values, it actually means it compares the references (not the properties and their values), which are only equal if they point to the exactly same object.

This article explains it in detail: https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0

BR Michael

like image 45
Michael S. Avatar answered Oct 05 '22 22:10

Michael S.


The fact that they're different is important in this scenario:

a={}; 
b={}; 
a.some_prop = 3;

At this point you'll obviously know that b.some_prop will be undefined.

The == or === operators thus allow you to be sure that you're not changing some object's properties, that you don't want changed

like image 42
vlad-ardelean Avatar answered Oct 05 '22 20:10

vlad-ardelean


They are different instances of objects, and can be modified independently. Even if they (currently) look alike, they are not the same. Comparing them by their (property) values can be useful sometimes, but in stateful programming languages the object equality is usually their identity.

like image 20
Bergi Avatar answered Oct 05 '22 20:10

Bergi