23

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.

11
  • 1
    I want the actual address space being referenced. I update my question . Commented Dec 3, 2012 at 14:56
  • 1
    I want to know where it is stored in memory. Also an internal representation of the pointer is fine. Commented Dec 3, 2012 at 15:00
  • 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 Commented Dec 3, 2012 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. Commented Dec 3, 2012 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 Commented Dec 3, 2012 at 15:11

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@DanieleB What do you need this for?
@DanieleB Sorry, my comment above was not very helpful. I think the answer is that consistently maintaining such an illusion would further complicate the language semantics (and this complexity would have to be incorporated by implementations).
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).

5 Comments

This is what is weird. If you can compare two references why can't you know their actual value?
@DanieleB Because there's no concept of memory allocation in Javascript visible to the developer, therefore no reason to know their location in memory
@DanieleB Probably because it would allow you to then treat pointers as integers and integers as pointers
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.
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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.