Skip to content

Instantly share code, notes, and snippets.

@XoseLluis
Created March 8, 2013 11:04
Show Gist options
  • Save XoseLluis/5115731 to your computer and use it in GitHub Desktop.
Save XoseLluis/5115731 to your computer and use it in GitHub Desktop.
Get the chain of internal prototypes for a given object
function getPrototypeChainOf(obj){
var chain = [];
var __prototype__ = Object.getPrototypeOf(obj);
//any protype chain ends in Object.prototype.__proto__, that is null
while (__prototype__){
chain.push(__prototype__);
__prototype__ = Object.getPrototypeOf(__prototype__);
}
return chain;
}
//let's test it
function Animal(){}
function Person(){}
Person.prototype = new Animal();
Person.prototype.constructor = Person;
function Asturian(){}
Asturian.prototype = new Person();
Asturian.prototype.constructor = Asturian;
var items = [
{},
new Animal(),
new Person(),
new Asturian()
].forEach(function(item){
console.log("prototype chain for instance of " + item.constructor.name + " type:\n"
+ getPrototypeChainOf(item).map(function(item){
return item.constructor.name;
}).join(", "));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment