class Rabbit1 {
constructor(name, color, type) {
if (arguments.length !== 3) throw new Error("Wrong length of arguments given");
this.ar1 = name;
this.ar2 = color;
this.ar3 = type;
this.testExtra = [];
if (!name) {
this.testExtra[0] = "This is in the constructor";
} else {
this.testExtra[0] = "Name is True";
}
}
speak(line) {
console.log(`The ${this.ar2} Rabbit, called '${this.ar1}' and says it\ 's a ${this.ar3} rabbit, Oh and it\'s saying ${line} too... ${this.testExtra}`);
}
speak2(speak) {
console.log("Hello " + this.ar1);
}
}
let blackR = new Rabbit1(false, "black", "gentle");
blackR.speak("Hello");
I’m asking this question because, if you take a look at
if (!name)
The binding name
is used, and it refers to the name
of the instance of the class (in this case blackR
).
So why do you we use this.name
when name
on its own refers to the name
value given in the instance of the class.
Please bear, with the poor indentation. Thanks.