Sometimes I get lost in prototype
chain of my JavaScript objects, so I would like to have a function that would print in a friendly way the prototype chain of a giv
This function shows prototype chain of any object clearly:
function tracePrototypeChainOf(object) {
var proto = object.constructor.prototype;
var result = '';
while (proto) {
result += ' -> ' + proto.constructor.name;
proto = Object.getPrototypeOf(proto)
}
return result;
}
var trace = tracePrototypeChainOf(document.body)
alert(trace);
tracePrototypeChainOf(document.body)
returns "-> HTMLBodyElement -> HTMLElement -> Element -> Node -> EventTarget -> Object"