I'm trying to understand why the following code works the way it does.
I think it's contradictory that logging killerRabbit.prototype gives undefined while at the same time it seems to be equal to Rabbit.prototype.
Does it has an associated prototype but no actual prototype as a property?.
In that case, how do I access those prototypes properties?
class Rabbit {
constructor(type) {
this.type = type;
}
speak(line) {
console.log(`The ${this.type} rabbit says '${line}'`);
}
}
let killerRabbit = new Rabbit("killer");
console.log(Object.getPrototypeOf(killerRabbit)==Rabbit.prototype);//true
console.log(killerRabbit.prototype);//undefined
If you want to access the prototype property on an instance, try this:
console.log(killerRabbit.__proto__);
And this property point to the prototype property of the based class.
console.log(killerRabbit.__proto__ === Rabbit.prototype); // true