If wouldn't be good to have that as a member method, but it's easy to implement as a standalone (recursive) function:
function protoTree(obj){
if(obj === null) return []
return [obj, ...protoTree(Object.getPrototypeOf(obj))]
}
It works like this (note that it also returns Object.prototype
, that a
inherits from.
var a = {a:1, b:2}
var b = Object.create(a);
b.a = 1
var c = Object.create(b);
c.c = 3;
var d = Object.create(c);
//Note that `protoTree` returns the prototype objects themselves, not the variable names they are stored in
protoTree(d); //returns: [c, b, a, Object.prototype]
protoTree(c); //returns: [b, a, Object.prototype]
protoTree(b); //returns: [a, Object.prototype]
protoTree(a); //returns: [Object.prototype]