You’ve finished References: Two Names, One Object, and the Okafors have next week’s plan, with last week left as it was. Everything you built rests on one picture: names are arrows to arrays and objects, and an array of day records holds arrows too. So you copy what you change, and share the rest.
In This Course, You Learned How To
- Edit arrays with
.slice()and.splice(), and predict when two names share one array or object - Compare records by identity and by contents, and unpack them with destructuring
- Merge records with spread, and write updates that leave the original untouched
- Build the Okafor family’s meal planner, and explain which parts of next week are new and which are shared
Things to Remember
const nextWeek = lastWeek;copies the arrow, not the array, so a change through either name shows through both.[...lastWeek]andlastWeek.slice()copy one level. The new array’s slots still point at last week’s day records, so the records inside stay shared.- Between arrays or objects,
===asks about arrows, never contents. It tells you which records the planner replaced, anddiffPlans()compares the cooks and meals to find the days that really changed. A fresh copy of Monday is new to===, butdiffPlans()doesn’t list it, because its cook and meals are the same. - Let a function change its caller’s data only on purpose, and give it a name that says so.
- To change something nested, copy every level on the path, and share the rest.