AssignmentCopiesObject

Misconception:

An expression like newObj = obj creates a copy of the object obj and assigns that copy to the variable newObj.

Incorrect

Assignment copies the object

Correct

Assignment copies the reference pointing to the object

Correction
Here is what's right.

An assignment of a reference to a variable copies the reference, not the object the reference points to.

Variables do not store objects themselves: they store references to objects. Thus, what gets copied is the reference, not the object the reference points to.

let p1 = new Pacman();
let p2 = p1;

The first line of the above code creates a Pacman object and assigns a reference to that object to variable p1. The second line copies the reference that is stored in p1 into variable p2. At the end of this code, there still is only a single Pacman object. And there are two variables, p1 and p2 that both point to that same object.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.