18

As the title of the question states, I was wondering if there is way to print the object memory address and if there is internal object hash? I know that I can use the object comparator:

var x = {id:1};
var y = x;
console.log(x === y); // => true

but I want to get a string/number representation of memory address and string representation of internal hash if there is one...

anchor
  • 755
  • 2
  • 8
  • 17
  • 5
    The first question is why you need that memory address ? What will it be useful for? – Ankit Agarwal Sep 19 '18 at 11:51
  • 1
    I don't think the EcmaScript standard define this sort of thing, so this would be an implementaiton detail of the actual Javascript engine you are using (V8 for Chrome / Node.JS, another for Mozilla Firefox...), so, in particular, there won't be anything in the language itself to know this sort of things. A debugger might. – Pac0 Sep 19 '18 at 11:54

1 Answers1

35

Neither of these pieces of information is available in JavaScript. If an environment were to provide it, it would be part of that environment's feature set, not JavaScript itself.

Also beware that the memory address of an object is not necessarily a constant thing across the lifetime of the object. In particular, modern JavaScript engines may allocate objects created within a function on the stack, and then copy them from the stack to the heap if the object is going to survive termination of the function. (They may well also move them around if necessary when doing garbage collection, though I don't specifically know that they do.) So even if you could get the address, it could well become invalid moments later. But if an environment made the information available, presumably it would come with environment-specific caveats in that regard.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875