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

Keep Both Sides of a Loan in Sync

In this video, the toy library’s lending desk starts recording loans. A loan links a toy to the member who has it, and each side needs to know about the other. You try the natural first attempt, setting only the toy’s borrower, and see Leo’s loans stay empty. Then you give the desk one lend() method that checks first and changes both sides.

Key Points

  • A loan has two sides. Each toy gets a borrower, which stays null until someone takes it, and each member’s record gets a loans array, empty at the start. A loan points the toy at the member and puts the toy in the member’s loans.
  • Setting one side doesn’t set the other. After train.borrower = leo, the train says Leo has it, but leo.loans.length is still 0, with no error and no warning. The borrower and the loans array are two separate properties on two separate objects. Nothing connects them except the code you write, unlike linked cells in a spreadsheet.
  • Adding the second line by hand everywhere doesn’t scale. Every script that lends would have to remember both lines, and one forgotten line brings the mismatch back.
  • One method changes both sides. The desk’s lend() finds the toy and the member, then checks before it changes anything. First, it keeps the desk’s age check: if the toy doesn’t suit the member, it says so and stops. Next, if the toy already has a borrower, it returns a message saying who has it and stops.
  • Only then does it change both sides. Only after those checks does it set the toy’s borrower and push the toy into the member’s loans. Then it returns the slip, with the due date from the policy.
  • A refused loan changes nothing. When Maya asks for the train Leo already has, the check refuses the loan before any change, so Maya’s loans stay empty.
  • The desk and the script share the objects. They reach the same toy and member objects by reference, so the script’s own names see what lend() did.
  • The rule for a two-way link is that one method changes it. That method checks first and then changes both sides, and nothing else assigns a borrower or changes a member’s loans. Reading them is fine. JavaScript won’t stop other code from changing them. Both borrower and loans are ordinary properties, and any code that reaches these objects can set them. Keeping every change inside lend() is up to you.

The Final Code

toy.js, with one new line in the constructor, this.borrower = null;:

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 The lending desk can write a loan slip. However, it doesn’t record the loan. When Leo Okonkwo takes the wooden train set home, the tracker should know who has the train, and what Leo has.

00:13 That’s a link with two sides. Each toy gets a borrower, which stays null until someone takes it. Each member’s record gets a loans array, empty at the start.

00:26 A first try records the loan on the toy. It sets the train’s borrower to Leo. Then the script logs who has the train, and how many loans Leo’s record shows.

00:38 Your turn: pause the video and predict how many loans Leo’s record shows.

00:48 Zero. The train says Leo has it, but Leo’s loans are still empty. There’s no error and no warning. The borrower and the loans array are two separate properties on two separate objects.

01:02 Setting one draws only that arrow. JavaScript never draws the other one for you. It isn’t like linked cells in a spreadsheet, where one change updates the other.

01:12 Nothing connects the two sides except the code you write.

01:17 You could add a push next to every line that sets a borrower. However, then every script that lends has to remember both lines, and one forgotten line brings the mismatch back.

01:29 So give the job to one place, a new lend method on the desk. It takes a toy’s id and a member’s name, and finds both in the desk’s lists. Then it checks before it changes anything.

01:43 First, it keeps the desk’s age check: if the toy doesn’t suit the member, lend says so and stops. Next, if the toy already has a borrower, lend returns a message saying who has it, and stops.

01:57 Only after those checks does it change the two sides. It sets the toy’s borrower to the member. And it pushes the toy into the member’s loans. Then it returns the slip, with the due date from the policy.

02:13 The lending script makes the train, Leo, and Maya Chen, and hands all three to the desk. The first lend gives Leo the train, due back in two weeks. Then Maya asks for the same train.

02:27 The borrower check finds Leo, so she gets a refusal.

02:31 Now read both sides through the script’s own variables. The train is with Leo. Leo’s loans hold one toy. And Maya’s loans are still empty, because the refusal came before any change.

02:45 The desk holds these same objects, shared by reference, so the script sees what lend did.

02:53 For a two-way link, the rule is that one method changes it. It checks first, then it changes both sides. Nothing else assigns a borrower or changes a member’s loans.

03:05 Reading them is fine. JavaScript won’t stop other code from changing them. Both borrower and loans are ordinary properties, and any code that reaches these objects can set them.

03:15 Keeping every change inside lend is up to you. So a loan is two arrows, and lend moves both of them, or, when a check says no, neither. Next, in Exercise: Run the Lending Desk, you’ll get the desk ready for a busy Saturday, lending toys and taking them back so every toy and every member always agree.

Become a Member to join the conversation.