Well, it is. Ish. Not entirely beautiful, but it is possible. You’d need to write a function to look at each ancestor in the prototype chain, make sure it’s a thing, and return that thing. So if I had something like this:
function Animal() { }
Animal.prototype = {
kingdom: "Animalia",
eat: function() {
console.log("nom nom nom");
}
};
function Dog(name) {
this.name = name;
}
Dog.prototype = Object.create(Animal.prototype) ;
Dog.prototype.numLegs = 4;
const beagle = new Dog("Snoopy");
… at this point, beagle
has a prototype chain that looks like beagle -> Dog -> Animal -> Object
(because all objects, behind the scenes, are Object
s). And each of those are providing an property. beagle
has the name
property, Dog
has the numLegs
, and Animal
provides the kingdom
property.
Ideally, if we want to see them all, we can’t from the beagle
class. We need to write a function that would look at the current thing, see if it has a prototype and, if it does, call itself with that prototype. Easier to show than to explain:
const getPropertyTree = function(obj){
// so do we *have* an object being passed in, and does it have a valid prototype?
return obj && !!Object.getPrototypeOf(obj)
// if it does, return an array of all the other recursive calls, plus this object
? [...getPropertyTree(Object.getPrototypeOf(obj)), obj]
// if it does *not* meet both those conditions, we've reached the end of the
// prototype chain - return an empty array.
: []
}
console.log( getPropertyTree(beagle) );
// the above gives us this:
// [
// { kingdom: 'Animalia', eat: [Function] },
// { numLegs: 4 },
// { name: 'Snoopy' }
// ]
So we get an array containing a reference to each object in the prototype chain, letting us see what properties it might contain.
So stock, no. There is no way to see all the properties on a prototype chain. You can make something to show you those properties, as I have done, and it could be done differently, but the system has no in-built way, as far as I know (aside from using the Dev Tools in your browser of choice.
).
If you’d like to tinker with this live, see what you can do to change or improve it: https://replit.com/@TobiasParent/PrototypeTreeThing#script.js