21

Possible Duplicate:
How can I get the memory address of a JavaScript variable?

Is there a way, in javascript, to print the reference of an array?

What I want to do is to check if two arrays have the same reference, and it can be done like this post suggests: How to check if two vars have the same reference?

But is it possible to print the reference, if for instance I'm working with an array?

Example

var Array1=["hi"];
var Array2=["hello"];

var thesame = Array1==Array2;

the same value is false.

But can I print he Array1 reference with somethink like window.alert(@Array1); in javascript?

--UPDATE --

What I exactly want is the actual address space being referenced.

Community
  • 1
  • 1
Daniele B
  • 3,117
  • 2
  • 23
  • 46
  • How do you print a reference? – Asad Saeeduddin Dec 03 '12 at 14:53
  • with 'typeof()' I only get the response that it is an object. See here http://jsbin.com/ezokam/1/ – Daniele B Dec 03 '12 at 14:55
  • What are you asking, you want the actual address space being referenced, or do you want it to print 'Array'? – PinnyM Dec 03 '12 at 14:56
  • 1
    I want the actual address space being referenced. I update my question . – Daniele B Dec 03 '12 at 14:56
  • Why would you want to print the reference? Maybe there is another way to accomplish what you're trying to do. – Neil Dec 03 '12 at 14:56
  • 1
    I want to know where it is stored in memory. Also an internal representation of the pointer is fine. – Daniele B Dec 03 '12 at 15:00
  • What could you possibly need to know where they are stored in memory for? – Ian Dec 03 '12 at 15:02
  • 1
    The answer is that you are not able to get the location in memory, and you probably never will. So if you would explain what you need this for, we could probably help better. Asking why this isn't possible isn't going to help your problem, I'm sure, so explain what you're trying to do – Ian Dec 03 '12 at 15:04
  • 1
    @Ian there is no real application I'm working on. I'm trying to understand whats under the hood in javascript engines. If you could retrieve the addresses and/or assign some address to variables then it would be possible to work with a 'pointer like' programming style. Of course I know that javascript is not C, but some interesting hacks could come out. – Daniele B Dec 03 '12 at 15:07
  • 2
    @DanieleB Oh I definitely agree, but there's no exposure of memory allocation by Javascript engines, so you can't work with that. I think a point of Javascript is that you shouldn't be worrying about memory, and the fact that it's a little strange since it's run within a browser, not as a standalone. I'm also not sure if it would be a good idea, but that's everyone's opinion – Ian Dec 03 '12 at 15:11
  • for others who are asking "Why do you want to do that? " This is really a good curious question, He/She want to view JS under microscope :) that's why he needs that. – Sagar May 16 '21 at 12:48

2 Answers2

15

Javascript implementations are only standardised in so much as they follow the ECMA spec. The intricacies of memory storage could differ by browser, and are not made accessible to JS.

As far as your question of why is concerned: JS is a lightweight scripting language. It makes sense to delegate memory management and optimization tasks to the platform, whereas it would require unnecessary hoop jumping to expose an interface for this to you.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • 1
    Ok I understand from what you say that it is not possible to get the REAL memory location. But also an internal representation of the pointer/reference would be fine. What I can't figure out is what I wrote on the @Minko Gechev comment. – Daniele B Dec 03 '12 at 15:03
  • @DanieleB What do you need this for? – Asad Saeeduddin Dec 03 '12 at 15:06
6

If you want to see the value of some kind of pointer to the referenced type (array, object) - you can't.

For example in, Perl you have a scalar type which is a reference to something. When you print this scalar you can get something like a string representation of the pointer to the value. There's no analogy of that kind of scalar in JavaScript.

You can only check whether two references are pointing to the same thing or not.

As in all languages with GC, in JavaScript you cannot be sure that the same object will be with the same memory pointer along the program execution (it can be moved in the heap). As I mentioned, you don't have an abstraction of pointer as build-in type in the language. So showing the current reference may be is not very good idea, because it may be dynamical (depending on the architecture of the engine and especially the GC).

Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
  • 6
    This is what is weird. If you can compare two references why can't you know their actual value? – Daniele B Dec 03 '12 at 15:00
  • @DanieleB Because there's no concept of memory allocation in Javascript visible to the developer, therefore no reason to know their location in memory – Ian Dec 03 '12 at 15:02
  • @DanieleB Probably because it would allow you to then treat pointers as integers and integers as pointers – Neil Dec 03 '12 at 15:03
  • When you want to print (`console.log`, for example) something in JavaScript it's `toString` method is being called. There's no primitive type `reference` in JavaScript and default `toString` of all objects/primitives is already defined. For example you can open a terminal and try `d8` (Google's v8 engine). Try to execute: `print({});`. This will lead to `[object Object]`, you cant see the pointer. – Minko Gechev Dec 03 '12 at 15:03
  • 2
    I just forgot to add something. As in all languages with GC, in JavaScript you cannot be sure that the same object will be with the same memory pointer along the program execution (it can be moved in the heap). As I mentioned, you don't have an abstraction of pointer as buildin type in the language. So showing the current reference may be is not very good idea, because it may be dynamical (depending on the architecture of the engine and especially the GC). – Minko Gechev Dec 03 '12 at 15:12