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?

5
  • possible duplicate of How does variable assignment work in JavaScript? Commented Jul 26, 2013 at 3:21
  • I voted this as a duplicate because the answer in the question you linked is the answer to your question. Commented Jul 26, 2013 at 3: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. Commented Jul 26, 2013 at 5:38
  • Old or no, the answer is the same. Commented Jul 26, 2013 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. Commented Jul 26, 2013 at 17:54

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.