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

Change a Caller's Data on Purpose

In this lesson, you moved a tweak to the Okafors’ meal plan into a function and found out when a function changes its caller’s data.

Key Points

  • The code that calls a function is its caller. When you pass an array to a function, the parameter gets the caller’s value, which is an arrow. So the parameter becomes one more arrow to the caller’s array.
  • removeDay(plan, position) splices the plan it’s given. It has no return statement, so a call to it gives back undefined, yet removeDay(nextWeek, 4) leaves nextWeek with six days, because .splice() follows the arrow in plan to the array nextWeek points at.
  • When the function ends, the parameter and its arrow disappear. The change to the array stays.
  • Watch out if you try to empty the caller’s array from a function like clearWeek(plan) by reassigning its parameter, as in plan = [];. That only swings the parameter’s own arrow to a new array, which is thrown away when the function ends. The caller’s array keeps its days. To give the caller a new array, return it, and let the caller store it.
  • removeDay() keeps last week safe only if every caller remembers to copy first. Forget the copy once, and removeDay(lastWeek, 4) splices Friday out of last week.
  • withoutDay(plan, position) does the copying itself: it copies the plan with [...plan], splices the copy and returns it. const nextWeek = withoutDay(lastWeek, 4); gives next week six days and leaves last week with seven.
  • Use a function that changes its array, like removeDay(), on an array the caller means to change. Use one that returns a new array, like withoutDay(), when the caller needs its original.
  • The rule: a function changes its caller’s data only on purpose, with a name that says so. Otherwise, copy every level it changes, and return the copy. withoutDay() only takes a slot out of the outer array, so a one-level copy is enough there.

Final Code

The final plan.js has both helpers and makes next week with the one that leaves last week alone:

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 In the last lesson, you copied every level of the plan that next week changes. So far, each of those changes has happened in the script’s top-level code. Now the planner is growing, and the Okafors want each tweak in a function of its own.

00:15 Here’s plan.js again, with last week’s seven day records. The new function, removeDay(), takes a plan and a position. It splices one record out of the plan at that position.

00:26 It has no return statement, so a call to it gives back undefined, which the script ignores. Next week starts as a copy of last week, made with spread syntax.

00:37 Then the script calls removeDay() with next week and four, which is Friday’s position. The last two lines log how many days each week has. Your turn: pause the video and predict how many days next week has now.

00:58 Next week has six days. Run the file, and the terminal shows six days for next week and seven for last week.

01:06 So a function that gives back only undefined still changed next week. To see how, draw the run. Before the call, last week and next week point at two arrays, each with seven slots.

01:20 When you call removeDay(), its parameter, plan, gets next week’s value. That value is an arrow, so plan becomes a second arrow to next week’s array.

01:30 Splice follows plan’s arrow and removes Friday from that array. When the function ends, plan and its arrow disappear. Next week still points at the array, which now has six days.

01:42 The code that made the call is the function’s caller, and it sees the change.

01:48 Watch out if you try to empty next week from a different function, clearWeek(). It reassigns its parameter: plan = [];. That only swings plan’s own arrow to the new array, which is thrown away when the function ends.

02:02 Next week keeps its six days. To give the caller a new array, return it, and let the caller store it.

02:10 removeDay() keeps last week safe only if every caller remembers to copy first. Forget the copy once, and it splices Friday out of last week. So write a safer helper, withoutDay(), that does the copying itself.

02:25 It takes the same plan and position. First, it makes a copy of the plan with spread syntax. Next, it splices the record out of the copy. Then it returns the copy to the caller.

02:37 Now the script doesn’t copy last week first. It passes last week straight in, and next week catches whatever comes back.

02:45 Draw this call too. Plan becomes a second arrow to last week’s array. The name copy points at a new array, and splice removes Friday from that array only.

02:56 Return hands the copy’s arrow back, and next week keeps it. Run the file. Next week has six days, and last week still has seven.

03:07 Now you have both kinds of helper. removeDay() changes the plan it’s given, and its name says so. Use it on an array the caller means to change, like next week.

03:17 withoutDay() leaves its plan alone and returns a new one. Use it when the caller needs its original, like last week. So the rule is that a function changes its caller’s data only on purpose, with a name that says so.

03:32 Otherwise, copy every level it changes, and return the copy. withoutDay() only takes a slot out of the outer array, so a one-level copy is enough there.

03:43 You’ve seen a parameter point at the caller’s array, one helper change that array, and another hand back a new one. Next comes Exercise: Copy and Edit the Week, where you’ll write planner helpers that make next week’s changes and leave last week as it was.

03:58 You’ll write and run the code right in your browser, and automated tests check your helpers.

Become a Member to join the conversation.