Skip to content

Instantly share code, notes, and snippets.

@Witiko
Last active December 22, 2015 10:59
Show Gist options
  • Save Witiko/6462356 to your computer and use it in GitHub Desktop.
Save Witiko/6462356 to your computer and use it in GitHub Desktop.
A user-defined Object2 object adding the support of multiple prototypal inheritance.
function Object2() {
this.prototypes = arguments;
this.attributes = {};
}; Object2.prototype.get = function(key) {
return this.attributes[key] || (function(array) {
for(var i = 0, l = array.length, result; !result && i < l; i++)
result = array[i].get(key); return result;
})(this.prototypes);
}; Object2.prototype.set = function(key, value) {
this.attributes[key] = value;
return this;
};
/*
var a = new Object2().set("a", 1).set("b", 2);
var b = new Object2().set("c", 3).set("d", 4);
var c = new Object2(a, b).set("e", 5);
c.get("a") + c.get("b") + c.get("c") + c.get("d") + c.get("e") === 15
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment