AOP

by southerd

JavaScript

function aspect(object) {
    extend(object, {
        _yield: null,
        _rv: {},
        before: function(method, f) {
            var original = this[method];
            this[method] = function() {
                f.apply(this, arguments);
                return original.apply(this, arguments);
            };
        },
        after: function(method, f) {
            var original = this[method];
            this[method] = function() {
                this._rv[method] = original.apply(this, arguments);
                return f.apply(this, arguments);
            }
        },
        around: function(method, f) {
            var original = this[method];
            this[method] = function() {
                this._yield = original;
                return f.apply(this, arguments);
            }
        }
    });
}