this.
In the previous section, the constructor contained some variables and some started with the keyword this. The keyword this
indicates that the variables declared with the this.
are object variables. We will use these variables to get information out of the class.
What does this look like?
Here is the class.
class Dog {
constructor(name, breed, weight, eyeColor, hairColor ) {
this.name = name;
this.breed = breed;
this.weight = weight;
this.eyeColor = eyeColor;
this.hairColor = hairColor;
}
eat()
{
text(this.name + "is eating...", 100, 100);
}
sleep()
{
text(this.name + " is sleeping...", 200, 200);
}
}
Here is the object instantiation.
var leonberger = new Dog("Alice", "Leonberger", 70, "brown", "brown");
function setup()
{
createCanvas(800,600);
}
function draw()
{
background(0);
fill(255);
text(leonberger.name, 100,100);
}
You will see that the name “Alice” is printed to the screen.
You give it a try and see what happens.