js 實現 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 = func.before(function () {
	console.log(1);
}).after(function () {
	console.log(3);
});

func();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章