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

Objects, this and Classes (Summary)

You’ve finished Objects, this and Classes. Mei’s houseplants started as plain records. Now each one is a Plant that looks after its own data, and a Shelf tells her which ones need water today.

What You Can Do Now

  • Model things that hold data and behavior with classes.
  • Protect their data with private fields and accessors.
  • Predict what this refers to in any call.

Things to Remember

  1. An ordinary, unbound function gets this from how it’s called, not where it was written. In a method call such as goldie.water(), this is the object before the dot. If you pull the method off with const water = goldie.water and call water() with no dot, then in strict code this is undefined.
  2. An unbound method passed as a callback loses this. In plants.forEach(shelf.waterPlant), waterPlant() loses the shelf, because forEach calls it with no dot. Call it on the object inside a wrapper, as in (plant) => shelf.waterPlant(plant), or bind it.
  3. Arrows take this from the code around them, so use them for callbacks in methods, not as methods.
  4. The new keyword makes an object, runs the constructor with this as that object, and hands it back. Shared methods live one step up the prototype chain, on Plant.prototype.
  5. Private fields, getters and setters let a class guard its own data. A private field hides the day count. A getter checks whether a plant needs water, and a setter rejects a negative interval.

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 Objects, this and Classes. Here’s what you can do now. You can model things that hold data and behavior with classes. You can protect their data with private fields and accessors.

00:14 And you can predict what this refers to in any call.

00:19 First, Mei’s plants got methods. In a method call, this is the object before the dot. Called with no dot, in strict code, this is undefined.

00:31 Passed to forEach, the shelf’s waterPlant method lost the shelf, because forEach calls it with no dot. An arrow wrapper put the shelf back before the dot, and every plant got watered.

00:45 Then came classes. The new keyword makes an object, runs the constructor with this as that object, and hands it back. Shared methods live one step up the chain, on the prototype.

00:58 A private field hid the day count. Accessors did the rest. A getter checked whether a plant needs water, and a setter rejected a negative interval. Before watering, the tracker tells Mei to water Goldie and Lily.

01:15 Five things to remember. An ordinary, unbound function gets this from how it’s called, not where it was written. An unbound method passed as a callback loses this, so call it on the object inside a wrapper, or bind it.

01:31 Arrows take this from the code around them, so use them for callbacks in methods, not as methods. And for classes: new builds each plant, and the class guards its own data.

01:46 To revisit plain objects, see the Records With Objects course.

01:51 Four records became a tracker that guards its own data. Thanks for watching.

Become a Member to join the conversation.