Look the code below:
class Person {
constructor() {}
}
class Passenger extends Person {
constructor() { super(); }
}
const john = new Passenger();
console.log(Object.getPrototypeOf(john));
What was printed into DevTools console in Chrome (111.0.5563.147) and Edge (111.0.1661.62) was:
Should not it be Passenger
? with such a meaning like the prototype of Passenger?
Obviously, it's not a problem with the prototype itself because:
console.log(Object.getPrototypeOf(john) === Passenger.prototype) \\log true
console.log(Object.getPrototypeOf(john) === Person.prototype) \\log false
I guess it's just a matter of what DevTools developers have chosen to print but if so... is not it a bit confusing?
Does Person
make any sense or I'm missing something here?
BTW Firefox (111.0.1) prints just Object
which is pretty fair and neutral
Well probably no. Logging
john
(anew Passenger()
) would print an object that is "a Passenger". But printingPassenger.prototype
? It's a hard question. The object you've logged is not aninstanceof Passenger
.Yes it makes some sense,
Passenger.prototype
(the object you've logged) is aninstanceof Person
, as it does inherit fromPerson.prototype
. Just as if you had logged anew Person()
instance (except thatPassenger.prototype
was never initialised by thePerson
constructor).Yes, it's their decision. In most circumstances, this approach is very useful. And yes, it is a bit confusing for prototype objects.