Yanti at the savings house’s door is the rupiah accessor: the getter counts the coins in the window, the setter turns rupiah into coins. What happens when the…
Why it matters
Getters/setters keep derived values always in sync (fullName, a cart total) and can validate input on write, without calling a method. They are used in classes, reactive stores (Vue, MobX), and clean object APIs.
The key idea
getter/setter = a property that quietly runs a function, accessed without parentheses
Common mistakes
Calling a getter with ()tabungan.rupiah() throws: the getter’s result (a number) is not a function. Access it without parentheses: tabungan.rupiah.
A setter writing to its own propertyset rupiah(r) { this.rupiah = r } calls itself until the stack runs out (RangeError). Store into another field, such as this.koin.
Heavy work or side effects in a getter Callers expect a getter to be as cheap as a field and read it many times. Keep getters light and free of side effects.
Try it in this playground
Read tabungan.rupiah Its getter runs once and counts koin × 500. No parentheses.
Write tabungan.rupiah = 5000 Its setter changes koin; rupiah itself is not stored.
Call it with parentheses: tabungan.rupiah() The getter runs first, then its number is called: a TypeError.
Drop the setter, then write A getter only = read-only. In strict code, writing throws a TypeError.