码迷,mamicode.com
首页 > 编程语言 > 详细

javascript实现aop

时间:2019-02-26 13:45:34      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:turn   on()   type   原理   ref   fun   arguments   实现   prototype   

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();

javascript实现aop

标签:turn   on()   type   原理   ref   fun   arguments   实现   prototype   

原文地址:https://www.cnblogs.com/cyrus-br/p/10436521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!