javascript实现aop

javascript实现aop的基本实现原理

        Function.prototype.before = function(beforefn){
            var _this = this; //  记录原函数的引用
            return function(){
                beforefn.apply(this, arguments);
                return _this.apply(this, arguments);
            }
        }

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

        var fun = function(){
            console.log("我是测试函数");
        }

        fun = fun.before(function(){
            console.log("之前执行");
        }).after(function(){
            console.log("之后执行");
        })

        fun();

猜你喜欢

转载自www.cnblogs.com/cyrus-br/p/10436521.html