A getter lets a property read run a method and return its result. This keeps an API’s surface simple—read `ninjaCollection.firstNinja` like a value—while allowing the value to be computed or other actions to run. section 9.1.1
An ordinary method named `getFirstNinja` is only a getter by convention; the name changes nothing about how it works. A true getter uses `get` in an object literal or class definition. Reading the property invokes it, without parentheses. A paired setter runs when that property is assigned. section 9.1.1
Listing 9.1 Defining getters and setters in an object literal
const ninjaCollection = {
ninjas: ["Yoshi", "Kuma", "Hattori"],
get firstNinja() { #A
console.log(`Getting firstNinja: ${this.ninjas[0]}`); #A
return this.ninjas[0]; #A
}, #A
set firstNinja(newNinja: string) { #B
console.log(`Setting firstNinja: ${newNinja}`); #B
this.ninjas[0] = newNinja; #B
}, #B
};
console.assert( #C
ninjaCollection.firstNinja === "Yoshi", #C
"Yoshi is the first ninja", #C
); #C
ninjaCollection.firstNinja = "Hachi"; #D
console.assert( #E
ninjaCollection.firstNinja === "Hachi" && #E
ninjaCollection.ninjas[0] === "Hachi", #E
"Now Hachi is the first ninja", #E
); #E
The getter returns the first ninja when `firstNinja` is read; the example also shows a setter updating the underlying array when the property is assigned. section 9.1.1
Use `Object.defineProperty` to add an accessor to an object that already exists. It is more verbose than defining `get` in an object literal, but supports dynamic changes, such as overriding a value in a simulated DOM during testing. section 9.1.1

