In the last exercise, you wrote a Plant class that keeps each plant’s own watering data. There’s a weakness in it, though. Any line of code that has a plant can change its data, so a slip such as goldie.daysSinceWatered = -3 in some other file gives Goldie a count that makes no sense, and the class never finds out.
In this lesson, you’ll declare a plant’s log and count as fields at the top of the class and make the days-since-watered count private, so only the class’s own methods can read or change it. You’ll also see exactly what happens when outside code tries to reach in. Start with the part of a plant’s data that doesn’t come from the constructor’s arguments at all.
Declare a Plant’s Data as Fields
Mei wants each plant to keep a watering log: the day count each time she waters it, so she can see whether she’s keeping to the interval. A log starts empty for every plant, so it isn’t something you’d pass to new Plant(...).
You could write this.wateringLog = []; in the constructor. A class also lets you declare it at the top of its body, as a field: a name, an equals sign, and a starting value, called its initializer. To see when that initializer runs, save this short demo as fields.js. Its Plant is a toy with only a name and a log:
fields.js
class Plant {
wateringLog = [];
constructor(name) {
console.log(`${name}'s log at the start:`, this.wateringLog);
this.name = name;
}
}
const goldie = new Plant("Goldie");
const moss = new Plant("Moss");
goldie.wateringLog.push(6);
console.log("Goldie's log:", goldie.wateringLog);
console.log("Moss's log:", moss.wateringLog);
Run it from your plant-tracker folder:
$ node fields.js
Goldie's log at the start: []
Moss's log at the start: []
Goldie's log: [ 6 ]
Moss's log: []
The first two lines show the order of events inside new Plant(...). The field is set on the new object first, and only then does the constructor body run, so its first line can already read this.wateringLog. The last two lines show that the initializer runs again for every plant. Goldie and Moss each get their own array, so pushing to Goldie’s log leaves Moss’s empty.
So here, a field gives the same result as a this.wateringLog = []; line at the start of the constructor. Both make an ordinary property of the plant. The two aren’t identical in every case, but that’s all you need about the difference for now. What a field changes is where you declare the data. With the declaration at the top of the class, you can see what data a plant holds before reading any method. You can delete fields.js now.
Make the Count Private
Fields also let you make data private. A name that starts with #, such as #daysSinceWatered, makes a private field: only code written inside the class’s braces can use it.
A private field must be declared in the class body, and every use of it keeps the #, as in this.#daysSinceWatered. JavaScript doesn’t mind where in the body the declaration sits, but this lesson keeps every field at the top so you see a plant’s data before any method.
Open plant.js and replace its contents with this version. It declares the log and the private count as fields, and methods that use the count now reach it through this.#daysSinceWatered: