高阶函数 - aop面向切面编程

        Function.prototype.before = function(beforefn) {
            var __self = this;
            return function() {
                beforefn.apply(this, arguments);
                return __self.apply(this, arguments);
            };
        };

        Function.prototype.after = function(afterfn) {
            var __self = this;
            return function() {
                var ret = __self.apply(this, arguments);
                afterfn.apply(this, arguments);
                return ret;
            };
        };

        var func = function() {
            console.log(2);
        };

        func.before(function() {
            console.log(1);
        }).after(function() {
            console.log(3);
        })();

 

posted @ 2017-07-11 16:47  Sorrow.X  阅读(271)  评论(0编辑  收藏  举报