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();
posted @ 2019-02-26 12:43  Cyrus_Br  阅读(130)  评论(0编辑  收藏  举报