javascript AOP实现
参考:http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541578.html
function Person(){ this.say = function(name,lang){ console.log("我的名字叫做"+name+",专注于"+lang+"..."); } } (function() { //AOP(面向切面编程)的javaScript实现 jsAOP = { /** * target 切入的目标对象 * method 切入的目标函数(相对于target) * advice 切入点执行函数 * **/ 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 p = new Person (); p = jsAOP.before(p,"say",function() { console.log("请你介绍一下自己!"); }); p = jsAOP.after(p,"say",function() { console.log("嗯,不错,明天来上班吧!"); }); p = jsAOP.around(p,"say",(function() { var index = -1; return function() { index++; if(index == 0) { console.log("介绍开始!"); }else{ console.log("介绍结束!"); index = -1; } }; }())); p.say("赖祥燃","javascript"); };
参考:http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541578.html
关注我,领取免费的前端学习资料,获取1个月免费VIP服务(包括简历优化、模拟面试、基础突击、大牛1v1辅导等)
转载请注明出处,谢谢!