freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #17 of 61

getters · setters · accessors

LambdaVSThe Mutator
Lambda saysGetters and setters define computed properties that run code on access or assignment.

Inside an object or class, get fullName(){…} runs when you read obj.fullName (no parentheses), and set fullName(v){…} runs when you assign it. They let a property look like data but compute or validate behind the scenes — useful for derived values and guarding writes.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleAdd a get fullName that joins first and last, and a setter that splits a string back.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const p={first:'Ada',last:'Lovelace',get full(){return this.first+' '+this.last;}};
document.getElementById('o').textContent=p.full;</script></body></html>
▶ Open the interactive comic issue
‹ Computed Property NamesArrays · Ordered Lists ›