Created
March 8, 2013 11:04
-
-
Save XoseLluis/5115731 to your computer and use it in GitHub Desktop.
Get the chain of internal prototypes for a given object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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