Skip to content

Instantly share code, notes, and snippets.

@bpceee
Forked from LukeChannings/printPrototypeChain.js
Created December 13, 2015 09:09
Show Gist options
  • Save bpceee/da19b27bcd3ada04f92f to your computer and use it in GitHub Desktop.
Save bpceee/da19b27bcd3ada04f92f 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(" -> ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment