javascipt的AOP

本文介绍了一种在Prototype JavaScript库中实现面向切面编程(AOP)的方法。通过扩展Event对象,实现了方法级别的前置、环绕和后置通知。此外,还提供了如何使用该实现的具体示例。

很多实现了,基于jquery和prototype的都有。

http://code.google.com/p/jquery-aop/
http://ajaxian.com/archives/eventobservemethod-more-aop-for-javascript
http://code.google.com/p/ajaxpect

 

PS:基于prototype的http://___.constantology.com/observe_method/  不能访问,提供http://___.constantology.com/download/observe_method/observemethod.js 的缓存内容。

 

Object.extend(Event, {
			AFTER : 'after',
			AROUND : 'around',
			BEFORE : 'before',
			observeMethod : function(scope, method, callback, aspect) {
				scope.listeners = $H(scope.listeners || {});
				if (!scope.listeners[method]) {
					scope.listeners[method] = this.setAspects();
					var __method = scope[method];
					scope[method] = function() {
						Event.dispatchCustomEvent(scope,
								scope.listeners[method].before, arguments);
						Event.dispatchCustomEvent(scope,
								scope.listeners[method].around, arguments);
						var args = $A(arguments);
						var r = __method.apply(scope, arguments);
						args.push(r);
						Event.dispatchCustomEvent(scope,
								scope.listeners[method].around, args);
						Event.dispatchCustomEvent(scope,
								scope.listeners[method].after, args);
						return r;
					}.bind(scope);
				}
				aspect = aspect || this.AFTER;
				if (scope.listeners[method][aspect].indexOf(callback) < 0)
					scope.listeners[method][aspect].push(callback);
			},
			purgeMethodListeners : function(scope) {
				if (arguments.length > 0) {
					if (arguments.length > 1)
						scope.listeners[arguments[1]][arguments[2]] = [];
					else
						scope.listeners[arguments[1]] = this.setAspects();
				} else
					scope.listeners.each(function(listener) {
								listener = this.setAspects();
							}.bind(this));
			},
			stopObserveMethod : function(scope, method, callback, aspect) {
				aspect = aspect || this.AFTER;
				scope.listeners[method][aspect] = scope.listeners[method][aspect]
						.without(callback);
			},
			dispatchCustomEvent : function(scope, events, argv) {
				var args = argv;
				if (events instanceof Array)
					events.each(function(func) {
								func.apply(this, args);
							}.bind(scope));
				else
					events.apply(scope, args);
			},
			setAspects : function() {
				return {
					after : [],
					around : [],
					before : []
				};
			}
		});

 

具体调用:

var myFunc = function() { alert( 'hello world!' ); }; 
var myOtherFunc = function() { alert( 'total annihilation of world is inevitable!' ); }; 
Event.observeMethod( window, 'myFunc', myOtherFunc ); 

var myNewFunc = function() { alert( 'i hate corporatocracies!' ); }; 
var myBeforeFunc = function() { alert( 'yes it\'s true!' ); }; 
Event.observeMethod( window, 'myNewFunc', myBeforeFunc, Event.BEFORE ); 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值