Classes and Methods
Method Updates
Update an instance field inside a method.
Method Updates
method_update.js
const start = 10;
class Score {
constructor(start) {
this.value = start;
}
add(points) {
this.value = this.value + points;
return this.value;
}
}
const next = new Score(start).add(5);
console.log("start=" + start);
console.log("next=" + next);
method-update
Methods can update fields through `this`. Keep the update small and return the new value when callers need it immediately.