JS Implementation AOP
<script>
function Actsasaspect (obj,handlers) {
if (typeof obj = = ' function ') {
obj = Obj.prototype;
}
for (var methodName in handlers) {
var _handlers = Handlers[methodname];
For (var handler in _handlers) {
if (handler = = ' Before ' | | handler = = ' after ') && typeof _handlers[handler] = = ' function ') {
Eval_r (Handler) (Obj,methodname,_handlers[handler]);
}
}
}
function before (obj,method,f) {
var original = Obj[method];
Obj[method] = function () {
F.apply (this, arguments);
Return original.apply (this, arguments);
}
}
function after (Obj,method, f) {
var original = Obj[method];
Obj[method] = function () {
Original.apply (this, arguments);
Return f.apply (this, arguments);
}
}
}
Actsasaspect (test,{' say2 ': {before:function () {alert (' before ');},after:function () {alert (' after ');}});
FunctionTest () {}
Actsasaspect (test,{' say2 ': {before:function () {alert (' before ');},after:function () {alert (' after ');}});
Test.prototype.say2 = function (s) {
alert (s);
}
Actsasaspect (test,{' say2 ': {before:function () {alert (' before ');},after:function () {alert (' after ');}});
var t = newtest ();
Actsasaspect (t,{' say2 ': {before:function () {alert (' before ');},after:function () {alert (' after ');}});
T.say2 ("Hello2");
</script>
Because Sina's editor is too poor, to change the Eval_r to Eval can be run
Support for class and object two types of AOP, which is based on modifications to the prototype, which are method modifications to the object
JS Implementation AOP