Skip to content

Instantly share code, notes, and snippets.

@LukeChannings
Last active December 20, 2016 08:42
Show Gist options
  • Save LukeChannings/5060084 to your computer and use it in GitHub Desktop.
Save LukeChannings/5060084 to your computer and use it in GitHub Desktop.
prints the prototype chain for a constructed object.
/**
* prints the prototype chain for a constructed object.
* @param {Object} a constructed object.
* @param asArray {Boolean} if true, the chain will be returned as an array.
*/
function printPrototypeChain(instance, asArray) {
var chain = []
while (instance = Object.getPrototypeOf(instance)) {
var name = instance.constructor.toString().match(/f.+n (.+)\(/)
chain.push(name && name[1]? name[1] : "(anonymous function expression)")
}
return asArray? chain : chain.join(" -> ")
}
@LukeChannings
Copy link
Author

Example output:

var a = printPrototypeChain(document.createTextNode("Hello World"))

a // "Text -> CharacterData -> Node -> Object"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment