Is there a pointer in Javascript?

Asked

Viewed 1,711 times

14

I was programming and I found something interesting but I was left with doubt in a situation.

I created an object and referenced it so:

a = {a:1,b:2}

And then I created another object and assigned the 'a' value on it:

b = a

Only when I changed the value of b, the values of a also changed.

This happened because I assigned not only the values of a for b but because I assigned the reference of one to another as well?

  • 1

    In Javascript, objects are at all times passed by reference, because, in fact, its value is always the reference and not the value itself

1 answer

11


Pointer exposed to you does not exist, but indirect, values accessed by reference have. It would be almost impossible to make some useful code without having such a form.

Pointer is an indirect access mechanism to a value and some languages expose it to the user (the programmer), others prefer to keep them opaque to simplify the language.

So what you’re using is a reference value that internally uses a pointer to cause indirect, but not exposed. So you assigned the reference, even if you’re not seeing this, and so you’re accessing the same object pointed by two different variables with the same pointer, you assigned the same pointer to them, without even seeing it because of the opacity.

Behold What is the difference between pointer and reference?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.