-2

I'm new to javascript and thus the doubt. With primary data types like string this makes sense,

let a = "goat";
let b = a;
let a = "apple"
b; //"goat"

However with a custom object,

const item = this.head;
 this.head = this.head.next;
 return item.val;

Why does item still point to the same head, when head has moved and is pointing to something else?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
  • Objects are passed by reference. Strings are literals. When you set an object equal to another, they share the same address-space. – Mr. Polywhirl Jan 15 '19 at 18:32
  • @Mr.Polywhirl: Not pass *by* reference. The are represented *as* reference. There is a difference. https://en.wikipedia.org/wiki/Evaluation_strategy – Felix Kling Jan 15 '19 at 18:34
  • Why the downvote? – Melissa Stewart Jan 15 '19 at 18:39
  • I did not down-vote, but I assume it's because this is an elementary concept in Object-Oriented Programming (OOP). – Mr. Polywhirl Jan 15 '19 at 18:46
  • Not all languages that support OOP behave like JS. – Some random IT boy Jan 15 '19 at 18:48
  • Your question is why does `item.val !== this.head.val` after you do the assignment? Other people seem to think it is something else. – epascarello Jan 15 '19 at 18:48
  • Please provide a complete example. See [mcve]. If `this.head !== this.head.next` then `item` and `this.head` will be different after `this.head = this.head.next;`. Variable assignment always works the same, no matter the value you are assigning (i.e. it always works like in your first example). – Felix Kling Jan 15 '19 at 18:50

2 Answers2

1

No it doesn't do a deep copy.
You declared item as a const. It means it can't change the object it refers.

Also, you made const item = this.head;, now item points at the same object as this.head points. Then you did this.head = this.head.next;, it means this.head point on another object, while item still points at the first object.

Blue
  • 355
  • 1
  • 9
  • the behavior is the same even if I declare item as a variable. How does const help in this case. – Melissa Stewart Jan 15 '19 at 19:48
  • 1
    The point is not about const. The point is in the second paragraph of the answer. You probably expect the item to refer at the head, but it doesn't. It refers to the object head refers. If you change the object head refers item will still refer what it was refering – Blue Jan 15 '19 at 19:51
0

Edit: It seems you have a linked-list and you want to shift the list to the left by one node. Your logic seems to be some sort of implementation of Array.prototype.shift.

  1. You store a reference to the head (node A)
  2. You set the head to point to the node after the current head (A.next = B)
  3. If you inspect the current head, it will state that it is node B
  4. You return the value for the original head (node A)
  5. Garbage Collect (GC) removes node A because it becomes detached from the list

class Node {
  constructor(val) {
    this.val = val
    this.next = null
  }
}

class LinkedList {
  constructor(head) {
    this.head = head
  }
  add(node) {
    let start = this.head
    if (start == null) this.head = node
    else {
      while (start.next) start = start.next
      start.next = node
    }
    return this
  }
  /** Remove node from front of list and return its value */
  shift() {
    const item = this.head            // x = Z.head (A)
    this.head = this.head.next        // Z.head = A.head.next (B)
    console.log('(1)', this.head.val) // DEBUG: B <-- NEW
    return item.val                   // x.val (A -- Original head ref value)
  }
}

let listZ = new LinkedList()
  .add(new Node('A'))
  .add(new Node('B'))
  .add(new Node('C'))

console.log('(2)', listZ.shift())     // A -- No longer a node in the list
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132