I would prefer that I don't assign Derived = chainify (), so the api would be the same as yours in your question, but at the moment this is the best way to make it work. It works by replacing each method of an object with a method that calls the replaced method and moves along the parent chain, invoking their methods on that path.
function chainify() { return function () { var property; for (property in this) { if (typeof this[property] === "function") { this[property] = chain(this[property], property); } } function chain(method, method_name) { return function() { method(); var current = this; while (current = current.parent) { if (current.hasOwnProperty(method_name)) { current[method_name].apply(this, arguments); } } }; } } } var somelib = function() { }; somelib.inherit = function (derive, base) { derive.prototype = new base; derive.prototype.parent = base.prototype; }; var Base = function() { }; Base.prototype.foo = function() { console.log("base foo"); }; var Derived = chainify(); somelib.inherit(Derived, Base); Derived.prototype.foo = function() { console.log("derived foo"); }; d = new Derived(); d.foo();
numerek
source share