很多实现了,基于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 );
分享到:
相关推荐
主要介绍了JavaScript AOP编程,实例分析了javascript实现AOP编程的基本技巧,需要的朋友可以参考下
javascript AOP 实现ajax回调函数使用比较方便,需要的朋友可以参考下。
主要介绍了Javascript aop(面向切面编程)之around(环绕) ,需要的朋友可以参考下
主要给大家介绍了关于JavaScript中AOP的实现与应用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用JavaScript具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
装饰器-aop 使用ES7装饰器进行Java语言方面的编程。 安装 npm install decorator-aop 用法 将类分配给方面。 @Aspect.target(Example) class ExampleAspect extends Aspect { } 连接点 使用以下装饰器在方面上...
AOP面向切面编程的JavaScript实现
主要介绍了JavaScript的AOP编程,以实例形式分析了javascript面向切面编程的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
AOP编程技术分享,包括iOS和javascript等多个平台实现
主要介绍了使用AOP改善javascript代码,需要的朋友可以参考下
什么是AOP? AOP(面向切面编程)的主要...通常,在 JavaScript 中实现 AOP,都是指把一个函数“动态织入”到另外一个函数之中,具体的实现技术有很多,下面我用扩展 Function.prototype 来做到这一点。请看下面代码:
javascript面向切面编程,可以用来防止表单重复递交
AOP基本功能在JavaScript中的研究和实现.pdf
在使用JavaScript搜索简单的AOP库时,我们评估了几种解决方案,并决定使用Fredrik Appelberg出色的 我们发现,不是将JavaScript对象传递到namespaces参数中,而是需要传递对象原型,因此该方面会拦截我们对象方法的...
此资源为我个人毕业设计系统,商城系统源码,包含数据库设计,系统分析,技术(Spring MVC AOP)+MyBatis+Ajax+JavaScript