class Dancer {
constructor(name, grade, style) {
this._name = name;
this._grade = grade;
this._style = style;
}
get name () {
return this._name;
}
get grade () {
return this._grade;
}
get style () {
return this._style;
}
set style (newStyle) {
this._style = newStyle;
}
}
We can grab that code and paste it into the console. Once we hit Enter, the class definition is in memory and in state. We can define instances with it, until we clear the console.
jane = new Dancer("Jane", 20, "Ballet")
jill = new Dancer("Jill", 21, "Interpretative")
Again, those lines are typed into the command line and entered. Two new instants in state.
Oh wait. I forget to add those two variables. No fear. We can add them as attributes of the, you guessed it, constructor.prototype object.
Dancer.prototype.isPresent = false;
Dancer.prototype.isInjured = false;
Let’s test if our instances inherited these attributes (properties):
jane.isPresent = true;
jill.isInjured = true;
Okay, here we go with the whole shebang.
> class Dancer {
constructor(name, grade, style) {
this._name = name;
this._grade = grade;
this._style = style;
}
get name () {
return this._name;
}
get grade () {
return this._grade;
}
get style () {
return this._style;
}
set style (newStyle) {
this._style = newStyle;
}
}
<- undefined
> jane = new Dancer("Jane", 20, "Ballet")
<- Dancer {_name: 'Jane', _grade: 20, _style: 'Ballet'}
> jill = new Dancer("Jill", 21, "Interpretative")
<- Dancer {_name: 'Jill', _grade: 21, _style: 'Interpretative'}
> Dancer.prototype.isPresent = false;
<- false
> Dancer.prototype.isInjured = false;
<- false
> jane.isPresent = true;
<- true
> jill.isInjured = true;
<- true
> Dancer.prototype.status = function () {
return `Present: ${this.isPresent}\nInjured: ${this.isInjured}`;
}
<- ƒ () {
return `Present: ${this.isPresent}\nInjured: ${this.isInjured}`;
}
> jane.status
<- ƒ () {
return `Present: ${this.isPresent}\nInjured: ${this.isInjured}`;
}
> jane.status()
'Present: true\nInjured: false'
> console.log(jill.status())
Present: false
Injured: true
<- undefined
>
From what I can tell, we cannot write get or set methods outside of the class definition. This makes sense because they are special methods that are partly in the control of the interpreter. Logically, we have no way to intervene, nor should expect to. It’s a feature of the new Class defined syntax, and outside of the vanilla prototype environment.
Notice that I defined isPresent
and isInjured
as normal properties. They are not meant to have a getter or setter but be immediately poll responsive in vanilla fashion. They are still instance variables and will always need to be in context for each instance, hence, this.is...
.
Above I arbitrarily set the two dancers’ status. Let’s see if we can toggle them, in state, as before.
> Dancer.prototype.notPresent = function () {
this.isPresent = ! this.isPresent;
}
<- ƒ () {
this.isPresent = ! this.isPresent;
}
> Dancer.prototype.notInjured = function () {
this.isInjured = ! this.isInjured;
}
<- ƒ () {
this.isInjured = ! this.isInjured;
}
> jill.notInjured()
<- undefined // no return value as expected
> jill.status()
<- 'Present: false\nInjured: false'
>
This is tripping right along. Cool stuff from where I sit, but then I’m just inventing on the fly. Coding is fun, isn’t it? Let’s not forget that cool print method. Try it out and show us your results.
Dancer.prototype.print = function () {
console.log(
' Name:\t', this.name, '\n', 'Age:\t', this.age, '\n', 'Style:', this.style
)
}