Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

References: Two Names, One Object (Summary)

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] and lastWeek.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, and diffPlans() compares the cooks and meals to find the days that really changed. A fresh copy of Monday is new to ===, but diffPlans() 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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You’ve finished References: Two Names, One Object. In this course, you learned how to edit arrays with .slice() and .splice(), and predict when two names share one array or object.

00:12 You learned how to compare records by identity and by contents, and unpack them with destructuring. You learned how to merge records with spread, and write updates that leave the original untouched.

00:24 And you built the Okafor family’s meal planner, and explained which parts of next week are new and which are shared.

00:32 This course kept coming back to one picture, with names as arrows. An equals sign adds a second arrow, so a change through either name shows through both.

00:43 Spread builds a new array, but its slots still point at last week’s day records. The finished planner copies only the path to each change. Tuesday, Thursday, and Friday get new records, and the other four days stay shared.

00:59 And last week’s Friday dinner is still takeout pizza.

01:04 === spots new records, and diffPlans() compares cooks and meals. So a fresh copy of Monday is new, but not changed.

01:16 Things to remember. An equals sign copies the arrow, not the array. Spread and .slice() copy one level, so the records inside stay shared. Between arrays or objects, === asks about arrows, never contents.

01:33 Let a function change its caller’s data only on purpose, and give it a name that says so. And to change something nested, copy every level on the path, and share the rest.

01:46 The RealJS reference has pages on reference, mutability, spread syntax, and equality operators.

01:54 Thanks for watching.

Become a Member to join the conversation.