What are getters and setters — and why a field vanishes when you send it to the server

Here's a strange one that can eat an evening. You have a user object. In the console, user.fullName honestly shows "Anya Kim." You send the user to the server — and there's no fullName. None. As if it never existed.
Nobody deleted it. fullName just isn't a field — it's a getter. And getters have a couple of non-obvious habits.
What a getter is — in one line
A getter is a property that looks like a field but is actually a function. You read it without parentheses, and under the hood code runs every time.
class User {
constructor(first, last) {
this.first = first;
this.last = last;
}
get fullName() {
return this.first + " " + this.last;
}
}
const user = new User("Anya", "Kim");
user.fullName; // "Anya Kim" — no parentheses
Think of a departures board at a station. You look at it like a sign. But the sign is assembled the moment you look, from fresh data.
A setter is the flip side. A property that looks like writing to a field but actually runs code:
set fullName(value) {
[this.first, this.last] = value.split(" ");
}
Now user.fullName = "Borya Lee" splits the string into first and last name by itself.
Why the getter vanishes when you send it
Here's the answer. In a class, the getter doesn't live in the object itself but in its "blueprint" — the prototype. The object itself only holds first and last.
And JSON.stringify only takes the object's own fields. Checked in Node 22:
JSON.stringify(user); // {"first":"Anya","last":"Kim"}
No fullName. Same trick with spread: { ...user } drops the getter too. That's why the server never sees it — data travels as JSON, and the getter never made it into the JSON.
Funny thing: in a plain object literal, a getter does end up in the JSON, because there it's the object's own. The only difference is where it's declared. That's why this bug catches people off guard.
Three ways to fix it:
- Add a
toJSON()method to the class that returns the fields you need, includingfullName.JSON.stringifycalls it automatically. - Build the object to send by hand:
{ first: user.first, fullName: user.fullName }. - If nothing needs computing — make
fullNamea regular field.
The setter trap: endless writing
The second classic. The AI writes a setter that cleans up the value:
set name(value) {
this.name = value.trim(); // bug!
}
See the catch? Inside the setter, this.name = … calls the same setter again. Which calls it again. A split second later — Maximum call stack size exceeded. If you see it, look at the stack trace: the same line will be there hundreds of times.
The fix is a separate "inner" field: store the value in this._name or a private this.#name, and name the getter and setter name.
Why it matters to you
Getters are a normal, handy tool. But in AI-written code it pays to notice them:
- See
getbefore a method — remember it's a computation, not data. It won't save itself into JSON. - A getter should be cheap. People read it like a field, often in a loop. A heavy request inside a getter is a bad idea.
- A getter shouldn't change anything. Reading a property, you expect nothing to break. Side effects here are confusing.
Why doesn't fullName reach the server?I have a User class with a getter get fullName that joins first and last. When I send it via fetch with JSON.stringify, fullName does not reach the server. Explain why, and add a toJSON method to the class that includes fullName. Leave the other fields untouched.How do I make a property read-only?
Keep the getter and skip the setter. Then user.fullName = "…" changes nothing. In a plain script the assignment is silently ignored; in a module — and almost all modern code is modules — it throws Cannot set property fullName of #<User> which has only a getter. We checked both in Node 22. See that error — someone is trying to write to a computed property.
Why doesn't console.log show the getter?
In Node, console.log(user) prints only own fields: User { first, last }. The browser shows a getter as (...) and computes it on click. The value isn't lost — it just wasn't computed in advance.
Is a getter the same as a method?
Almost. It's a function, but you call it without parentheses, like a field. A getFullName() method does the same thing but honestly looks like an action.
Does Python have getters?
Yes, the @property decorator does exactly this: a method you read like a field. Same logic, different syntax.
When should I skip the getter?
When the value needs to be stored, sent or compared as data. Then a plain field is simpler. A getter is good for things always derived from other fields.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





