I have been looking for a while for an answer and never found one. Thought I'd post my question here.
In Java Script, when we create an object constructor like so:
function Car(speed){
this.speed = speed;
this.position = 0;
}
Car.prototype.move = function(){
this.position++
}
then we can access the prototype's properties like so:
console.log(Car.prototype)
// will output: Car { move: [Function] }
But when we declare the Car constructor like so (with ES6 classes)
class Car{
constructor(speed) {
this.speed = speed;
this.position = 0;
}
move(){
this.position++:
}
}
then strangely when we do this we get an empty object:
console.log(Car.prototype)
// will output: Car { }
From what I read, all properties declared in a ES6 class outside of the constructor method are assigned on the prototype of the object. So why can't we access the properties of the prototype using console.log(Car.prototype)?
I also know it is part of the prototype because when I do this, it works:
console.log(Object.getOwnPropertyNames(Car.prototype));
// output [ 'constructor', 'move' ]
So I know the move method is assigned to the prototype.
I just wonder why can't we access those properties using Objet.prototype like when we declare the constructor in a function instead of in a ES6 class?
Did I missed something, is this a bug?
I'm working in Node.js, if this information might be helpful
console.log(Car.protoype)does not outputCar { }, for me at least. It outputs{constructor: ƒ, move: ƒ} constructor: class Car, move: ƒ move(), __proto__: ObjectCar { }when I use ES6 classconsole.log(Object.getOwnPropertyNames(Car.prototype)).