您的位置:首页 > 编程语言 > Java开发

Aop javacript 实现

2013-05-26 21:59 169 查看
基本概念
1切入点:PointCut

理解:

需要控制、采取行动的那个位置,譬如需要在登陆时添加日志记录的话,登陆方法(login方法就被称为PointCut)

代码:

{target: window, method: 'alert'},

1通知:[b]advice [/b]

理解:

在切入点上施加的具体操作,譬如需要在登陆时添加日志记录的话,记录日志就通知。

代码:

其中匿名function就是一个通知。

$.aop.before( {target: window, method: 'alert'},

function(alertarguments/*这里是alert方法的参数*/) {

document.write("before alert,argument: " + alertarguments[0] + "<br />");

}

);

代码解读:

对象:

PointCut {target: window, method: 'alert'}

target:方法所属的对象

method :方法名

advice {
type: _before, value: advice }

type:通知类型

value:通知业务实现



过程



1. jQuery.aop.before( pointcut, advice) [b] ---> [b]weave( pointcut, { type: _before, value: advice } )[/b][/b]

逻辑:

1. 封装 用户定义advice对象,添加type通知类型属性

[b][/b]

2 . weave([b]织入切点 和 切点解析)-->weaveone(source, pointcut.method, advice);[/b]

逻辑:

1.获取source对象(方法所属的对象),切点target有原型属性的话就用原型属性,否则直接使用target

2.调用weaveone

3 weaveone

逻辑:

1. 执行通知方法

2. 返回执行切点方法

代码参考:

advice.value.apply(this, [arguments, method]);

return old.apply(this, arguments);


备注:其中 arguments 参数为 切点方法的参数集合

知识点:

1. js 方法定义类型

http://dancewithnet.com/2008/05/07/javascript-anonymous-function/

2. apply 方法的使用:

function sayColor(sPrefix,sSuffix){

alert(sPrefix+this.color+sSuffix)

}

var obj = new Object();

obj.color = 'red';

sayColor.call(obj,'The color is ',' a very nice color indeed');

sayColor.apply(obj,['The color is ',' a very nice color indeed']);

call和apply的第一个参数的作用就是把实体传入,告诉浏览器我是要在obj这个对象上执行sayColor,自然this指向了obj

http://www.cnitblog.com/yemoo/archive/2007/11/30/37070.aspx

/article/3482737.html 先执行 call中对象方法

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: