Skip to content

Instantly share code, notes, and snippets.

@matthewrobb
Last active December 27, 2015 00:39
Show Gist options
  • Save matthewrobb/7239518 to your computer and use it in GitHub Desktop.
Save matthewrobb/7239518 to your computer and use it in GitHub Desktop.
Recursive object descent and prototype reassignment
function inheritize(root){
Object.keys(root).forEach(function(key){
var branch = root[key];
if (typeof branch === "object") {
Object.keys(branch).forEach(function(key){
var leaf = branch[key];
if (typeof leaf === "object" && typeof root[key] === "object") {
// In the HOPEFULLY near future this would be replaced with:
// Object.setPrototypeOf(leaf, root[key]);
leaf.__proto__ = root[key];
}
});
inheritize(branch);
}
});
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment