1

So, I created a standard linked list class in js and it workds perfectly but this happens for example/.....

     var myRef = new LinkedList()  //let's say with vals 1->2->3   
     console.log(myRef) //2->3  They are both the same!!!! even though delete happens later in code!!!
     myRef.deleteFirst();

    console.log(myRef)  //2->3 They are both the Same!! 

    LinkedList.prototype.deleteFirst = function() {
       if (this.head.next == null) {
       return;
  }
  var dummy = this.head.next.next;
  var value = this.head.next.data;
  delete this.head.next;
  this.head.next = dummy;
  this.head.length -= 1;

  return value;

}
LoopyFoot
  • 11
  • 1
  • 1
    Related: [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – Felix Kling Feb 10 '17 at 14:16
  • Click on the `i` next to the console output and read the explanation there. – Felix Kling Feb 10 '17 at 14:20

2 Answers2

0

try this:

console.log(JSON.stringify(myRef));

This will print the stringify version of object. So instead of your object will get the content of object otherwise because myRef is a pointer its content will change and the log show the latest value.

Besat
  • 1,428
  • 13
  • 27
0

The console will have a reference to the object, which will update when the object updates. You can do this:

console.log(jQuery.extend(true, {}, myRef));

which will create an independent copy of your object. That will allow you to examine the different states.

JJFlash42
  • 131
  • 1
  • 7