JavaScript AOP编程实例
本文实例讲述了JavaScriptAOP编程。分享给大家供大家参考。具体如下:
/* //aop({options}); //By:adamchow2326@yahoo.com.au //Version:1.0 //Simpleaspectorientedprogrammingmodule //supportAspectbefore,afterandaround //usage: aop({ context:myObject,//scopecontextofthetargetfunction. target:"test",//targetfunctionname before:function(){//beforefunctionwillberunbeforethetargetfunction console.log("aopbefore"); }, after:function(){//afterfunctionwillberunafterthetargetfunction console.log("aopafter"); }, around:function(){//aroundfunctionwillberunbeforeandafterthetargetfunction console.log("aoparound"); } }); */ varaop=(function(){ varoptions={}, context=window, oFn, oFnArg, targetFn, targetFnSelector, beforeFn, afterFn, aroundFn, cloneFn=function(Fn){ if(typeofFn==="function"){ returneval('['+Fn.toString()+']')[0]; } returnnull; }, checkContext=function(){ if(options.context){ context=options.context; } if(typeofcontext[(options.target).name]==="function"){ targetFnSelector=(options.target).name; targetFn=context[targetFnSelector]; } elseif(typeofcontext[options.target]==="function"){ targetFnSelector=options.target; targetFn=context[targetFnSelector]; } if(targetFn){ oFn=cloneFn(targetFn); oFnArg=newArray(targetFn.length); returntrue; } else{ returnfalse; } }, run=function(){ context[targetFnSelector]=function(oFnArg){ if(aroundFn){ aroundFn.apply(this,arguments); } if(beforeFn){ beforeFn.apply(this,arguments);//'this'iscontext } oFn.apply(this,arguments); if(afterFn){ afterFn.apply(this,arguments);//'this'iscontext } if(aroundFn){ aroundFn.apply(this,arguments); } }; }; returnfunction(opt){ if(opt&&typeofopt==="object"&&!opt.length){ options=opt; if(options.target&&checkContext()){ if(options.before&&typeofoptions.before==="function"){ beforeFn=options.before; } if(options.after&&typeofoptions.after==="function"){ afterFn=options.after; } if(options.around&&typeofoptions.after==="function"){ aroundFn=options.around; } run(); } } }; })(); //testexamples //-----------------aopmodifyglobalfunction---------------// functiontest(name,age){ console.log("testfn.name="+name+"age:"+age); } aop({ target:"test", before:function(){ console.log("aopbefore"); }, after:function(){ console.log("aopafter"); }, around:function(){ console.log("aoparound"); } }); //run test("adam",6); //-----------------aoptestmodifymethodinanobject---------------// varmyobj={ myName:"testName", sayName:function(){ console.log(this.myName); }, childObj:{ age:6, say:function(){ console.log(this.age); } } }; aop({ context:myobj, target:"sayName", before:function(){ console.log("aopbeforesayname="+this.myName); }, after:function(){ console.log("aopaftersayname="+this.myName); }, around:function(){ console.log("aoparoundsayname="+this.myName); } }); //run myobj.sayName(); aop({ context:myobj.childObj, target:"say", before:function(){ console.log("aopbeforesayname="+this.age); }, after:function(){ console.log("aopaftersayname="+this.age); }, around:function(){ console.log("aoparoundsayname="+this.age); } }); myobj.childObj.say();
希望本文所述对大家的javascript程序设计有所帮助。