To illustrate how an implementation of MVC might look in JavaScript, let's build a very simple program. It will be a basic mutable number application that renders a simple UI where the user can see the current number and choose to update it via either incrementing or decrementing its value.
First, we can implement the logic and containment of our data using a Model:
class MutableNumberModel { constructor(value) { this.value = value; } increment() { this.value++; this.onChangeCallback(); } decrement() { this.value--; this.onChangeCallback(); } registerChangeCallback(onChangeCallback) { this.onChangeCallback = onChangeCallback; }}
In addition to storing the value itself, this class also accepts and relies upon a callback ...