javascript AOP的实现【练习】 web前端 杭州-小白
刚群里99大神童鞋,发了一个地址 司徒正美的博客,他们都是大神呢,这一篇内容是关于 javascript AOP的实现呢,这里小小的玩了一下,不错确实很强大.
代码如下:
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>aop练习</title> </head> <body> <input type="button" id="btn" value="go"></input> <script type="text/javascript"> /*target:被织入的对象 ,method:被织入的方法名字 ,advice: 通知函数*/ function Person(){ this.say=function(name,lang){ alert("您的名字叫"+name+"?是否专注于"+lang+"!"); } } function voice(){ alert("gogogo!"); } Aspects=function(){}; Aspects.prototype={ before:function(target,method,advice) { var original=target[method]; target[method]=function(){ (advice)(); original.apply(target,arguments); } return target; }, after:function(target,method,advice){ var original=target[method]; target[method]=function(){ original.apply(target,arguments); (advice)(); } return target; }, around:function(target,method,advice) { var original=target[method]; target[method]=function(){ (advice)(); original.apply(target,arguments); (advice)(); } return target; } } window.onload=function(){ //var t=new Person; //var a=new Aspects; //t=a.before(t,"say",function(){alert("请介绍一下自己");}); //alert("分割线"); //t.say("小白","javascript"); var btn=document.getElementById('btn'); btn.onclick=voice; var a=new Aspects; a.after(btn,"onclick",function(){alert("不许go!回来!");}); // a.after(btn,"onclick",function(){alert("不许go!回来!");}); } </script> </body> </html>
哈哈,感觉这个特别有用嘛。。嘿嘿.PS:继续努力….
原文详细地址:http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541578.html