9

I have a question out of curiosity. So I looked into how JS handles variable assignment and I get it. How does variable assignment work in JavaScript?

But the same principle doesn't seem to exhibit itself in the following code I am working on:

var temp = playlist1[0];
playlist1[0] = playlist1[1];
playlist1[1] = temp;

I know this is a standard way to swap array elements. But if temp is pointing at playlist1[0], and playlist1[0]'s contents are changed to playlist1[1]'s then how come I don't end up with two playlist1[1] values in a row?

Community
  • 1
  • 1
nemo
  • 593
  • 2
  • 8
  • 22
  • possible duplicate of [How does variable assignment work in JavaScript?](http://stackoverflow.com/questions/509579/how-does-variable-assignment-work-in-javascript) – Evan Trimboli Jul 26 '13 at 03:21
  • I voted this as a duplicate because the answer in the question you linked is the answer to your question. – Evan Trimboli Jul 26 '13 at 03:22
  • @EvanTrimboli I think the question is fair since it asks for further clarification on an existing question. The other question is old and probably not monitored anymore. – TGH Jul 26 '13 at 05:38
  • Old or no, the answer is the same. – Evan Trimboli Jul 26 '13 at 14:58
  • 1
    OP argues that he asked this question to further his understanding of the topic. Upon initial interpretation (not coming from a place of 'knowing already') the linked question does not clearly answer the question. Upon final interpretation (coming from a place of 'knowing already') then the OP sees how the linked question may help; as a reference text and not a learning text. But OP maintains that this question adds value to the linked question and deepens the learning as to avoid a probable misconception of the variable as object pointer concept. – nemo Jul 26 '13 at 17:54

4 Answers4

12

Not only variables are object pointers. All values (that are not primitives) are object pointers. So temp is an object pointer. playlist1 is a object pointer to an array object whose elements are object pointers. e.g. playlist1[0] is an object pointer, playlist1[1] is an object pointer, etc.

But if temp is pointing at playlist1[0]

This doesn't make sense. temp is an object pointer. It points to an object. playlist1[0] is not an object; it's an object pointer. temp = playlist1[0]; makes the object pointer temp point to the same object as object pointer playlist1[0].

If you know C, it is equivalent to something like this:

Object *playlist1[10];

Object *temp = playlist1[0];
playlist1[0] = playlist1[1];
playlist1[1] = temp;
newacct
  • 119,665
  • 29
  • 163
  • 224
  • 1
    OP's takeaway: array elements are not objects, they too are pointers to objects. This answered my question and deepened my understanding of the mechanisms. – nemo Jul 26 '13 at 17:50
2

This is consistent with the answer in the referenced question: You are just changing which object the variable points to - not the data it used to point to. Meaning temp is unaffected by the move to have playlist1[1] point to playlist1[2]. Temp retains the original value it pointed to when playlis1[1] and temp both pointed to it. Only playlist1[1] is updated

TGH
  • 38,769
  • 12
  • 102
  • 135
0

Because those are still references to elements in the array and not the elements themselves. So in the line:

playlist[1]=playlist[2]

You are not changing anything about temp. Contrast that with something like (assuming array elements were objects):

playlist[1].x=playlist[2].x

That is actually assigning the value of the object in the array, and if temp pointed to playlist[1], then temp.x would equal playlist[2].x

mckurt
  • 134
  • 7
0

say we have obj={l1:{l2:[1,2]},} and we want to address obj.l1.l2[1] using an array of levels like arr=["l1","l2",1] then :

Object.defineProperty(Object.prototype,'point',{
    value:function(arr){
        var rez=this;
        for(var s in arr){
            rez=rez[arr[s]];
            if(rez === undefined) return undefined;
        }
        return rez;
    }
});

So after defining "point" method (which is not enumerable to mess up everithing) we can use

obj.point(arr)

to get value 2

bortunac
  • 4,642
  • 1
  • 32
  • 21