References vs copies: why changing one object changes another
See why a second box, a parameter or a list leads to the same object rather than a copy, so a change made through one name is seen through every name. Make a real copy with new braces when a change must not reach the original, and tell the two apart with ===.
Fundamentals
TypeScript
References vs copies: why changing one object changes another. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
You share a shopping list with a friend by sending a link. They open
it, cross out eggs, and when you open the same link, eggs are crossed
out for you too. There is one list, and you both have a link to it. If
you had sent the file itself, as an attachment, they would have
crossed eggs out on their own copy, and yours would not have changed.
The last lesson ended on this question: when you hand milk to a
function, or put it in a second box, does the function or the box get
its own copy of the task, or the very same one? A box that holds an
object works like the link. By the end of this lesson you can tell,
for any line, whether two names share one object, and you can make a
real copy when you need one.
A box holds a number, but points at an object
In lesson 2.1, const before = done; kept its own 1 while done
went on to 3: a box holds its value, and = copies a number into
the new box. The same is true of text and of true and false.
An object is different. It can hold many properties and change
after it is made, so it does not sit inside a box. It lives in a
place of its own in memory, and the box holds only a link to it:
an arrow that points at the object. (This arrow is a line in the
pictures below, not the => of Module 5's arrow functions.) That
arrow is called a reference. So const other = rent; does not
copy the task. It copies the arrow, and now two boxes point at one
object: a shared object.
Here are four lines, and the boxes they leave behind:
#1 and #2 name the two objects in memory, and … stands for the
id (short for identifier, the number that names one task) and
title both of them have. Each box that points at an object says
which one, → #1; on a wide screen a line draws the arrow too. Press
Next variable to walk through the boxes, or let it play:
Variables
Values they share
const rent: Task points at shared value #1constrent: Task→ #1
const other: Task points at shared value #1constother: Task→ #1
const copy: Task points at shared value #2constcopy: Task→ #2
All 3 variables shown.Variable 1 of 3: const rent: Task points at shared value #1Variable 2 of 3: const other: Task points at shared value #1Variable 3 of 3: const copy: Task points at shared value #2
All 3 variables shown.
The change made through other happened to object #1, and rent
points at #1 too. copy points at an object of its own, so it still
says Monday. Run it without the copy line, and press Step to watch
line 9 change the task you read through rent:
It prints Friday, although no line wrote rent.due = …. There is
one task, and both names point at it.
This also says what lesson 6.2's const really locks. Lesson 6.2 said
the task is inside the box; more exactly, the box holds its arrow. A
const box can never get another arrow: rent = { … } is still
refused with TS2588 (TS for TypeScript, then the number of this kind
of mistake): Cannot assign to 'rent' because it is a constant. The
object at the end of the arrow can still change, through rent or
through any other name that points at it. A let box can be refilled:
it gets a new arrow, and the object it pointed at before is left as it
was.
A function gets the same object
Now the last lesson's question, asked of rent instead of milk. A
parameter is a box too, made when the function is called and filled
with the value you pass. For an object, that value is the arrow. Here
postpone puts a task off to another day:
It prints Friday. While postpone runs, after task.due = day,
rent and the two parameters look like this:
Variables
Values they share
const rent: Task points at shared value #1constrent: Task→ #1
let task: Task points at shared value #1lettask: Task→ #1
let day: string holds "Friday"letday: string"Friday"
#1{…,due: "Friday"}
Inside postpone, task points at rent's object; day holds its
own text
day got its own copy of the text, as before got its own 1 in
lesson 2.1. task got a copy of the arrow, so task.due = day
changes the very object rent points at. (A parameter can be refilled
like a let box, which is why the figure says let.) This is useful:
it is how a function changes your task on purpose, and postpone is
meant to. It goes wrong only when you did not expect it.
A list is an object too, so a box holds an arrow to it, and a function
that calls push on its parameter adds to your list. That is why
TaskKit's printList from lesson 5.6 sorts with toSorted. Its
parameter tasks points at the list the rest of TaskKit uses, not at
a copy. Lesson 4.4's sort reorders the list it is called on, so
tasks.sort(…) inside printList would reorder that list for every
other part of TaskKit too. toSorted makes a new list and leaves that
one alone.
A real copy
When a change must not reach the original, make a real copy: a
new object. New braces always make a new object, so write them out
and fill each property from the old one:
It prints Monday Friday, then true false. rent.id,
rent.title and rent.due are a number and two pieces of text, so
each is copied into the new object, and the two objects share
nothing. The last line uses lesson 2.3's ===. Between two objects
it compares the arrows, not the properties: other is the same
object as rent, and copy, with every property rent has, is
still another object.
A function can do the same: one that ends with
return { id: task.id, … }; gives back a new object, and leaves
the one it was handed as it was. The next lesson shows a much
shorter way to write such a copy.
Predict before you run
A week's plan is a list with the rent task in it. A loop moves
every task in the plan to Friday. Decide what the last line prints
before you run it.
Predict
What does the last line print?
The common mistake: a "before" kept with =
Before you change something, you often want to keep how it was, to
print what changed or to undo it. With an object, = does not keep
how it was.
Common mistake
Keeping the old task in a second box before changing it
You move the rent to Friday and want a line that says where it
came from, so you keep the task in before first:
before points at the same task as rent, so the change on line 9
is seen through both names.
Moved from Friday to Friday
The fix
Keep what you need. Here that is only the old date, which is text,
so it is copied:
It prints Moved from Monday to Friday. There is no error message to
search for: the program runs and prints the new value where you
expected the old one. When that happens, look for an = that put an
object into a second box, and ask which object each name points at. If
you need the whole task as it was, make a real copy with new braces
before the change.
Pass all three tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
Each exercise is checked in your browser. Press Check when you
think you have it; if you are stuck, the hints come one at a time.
Recap
Recap · 3 questions
Check yourself
rent points at a task. What does other = rent put in other?
Check yourself
A function sets task.due on its parameter. What changes?
Check yourself
copy has rent's properties and values. Is copy === rent?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
References and copies
What things are
Word
What it means
Reference
The arrow a box holds to an object
Shared object
One object that two or more names point at
Real copy
A new object, its properties filled from the old one
Code
Write
What happens
const before = done
A number or text: copied
const other = rent
The arrow is copied; one object, two names
postpone(rent, …)
The parameter points at rent's object
[rent]
The list holds rent's arrow
{ id: rent.id, … }
New braces: a new object
other === rent
true only for the same object
Rules
Rule
What it means
A change through one name
Is seen through every name for that object
const
Locks the arrow; the object can still change
Refilling a box
Moves that box's arrow, changes no object
Not a type error
TypeScript never warns about sharing
A "before"
Copy the value you need, or the whole object
Where to go next
You can now tell, for any line, whether it shares an object or
makes a new one: =, a parameter and a list share the arrow, and
only new braces make a new object. You can also check it with
===, and keep the old value before a change.
Next is Destructuring and spread (and why spread is shallow).
Writing out every property for a copy gets long once a task has
more of them. There you meet a short way to take properties out of
an object and to copy one, and find out where that short copy still
shares.
Before you move on, go back to same.ts, press Edit, and add a
third name for the task, const third = other;. Change due
through third, and predict what rent.due prints before you
press Run.
#1{…,due: "Friday"}
#2{…,due: "Monday"}
rent and other point at one object; copy points at its own
Exercise · Read the code
Which due date does rent keep
Task is the one from this lesson. What does the last line print?
moved should give back the task moved to another day, and leave
the task it is handed as it was. This program should print
Monday -> Friday, but it prints Friday -> Friday. Fix moved,
and leave the rest as it is.