1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 |
*//AOP ({options}); by:adamchow2326@yahoo.com.au//version:1.0/Simple aspect oriented programming module//support aspect-before, AF ter and around//USAGE:AOP ({context:myobject,//scope context of the target function. Target: "Test",//target funct Ion name Before:function () {//Before function would be run before the target function Console.log ("AOP before");}, Afte R:function () {//After function would be run after the target function Console.log ("AOP after");}, Around:function () { Around function'll is run before and after the target function Console.log ("AOP around"); } }); * * var AOP = (function () {var options = {}, context = window, ofn, Ofnarg, TARGETFN, Targetfnselector, Beforefn, AFTERFN, AROUNDFN, Clonefn = function (fn) {if (typeof Fn = = "function") {return eval (' [' +fn.tostring () + '] ') [0];} return null ; }, Checkcontext = function () {if (Options.context) {context = Options.context} if (typeof context[(options.target). Nam E] = = = "function") { Targetfnselector = (options.target). Name; TARGETFN = Context[targetfnselector]; else if (typeof context[options.target] = = "function") {targetfnselector = Options.target; targetfn = Context[targetfn Selector]; } if (TARGETFN) {ofn = Clonefn (TARGETFN); ofnarg = new Array (targetfn.length); return true;} else {return false;}}, R UN = function () {Context[targetfnselector] = function (Ofnarg) {if (AROUNDFN) {aroundfn.apply (this, arguments);} if (bef OREFN) {beforefn.apply (this, arguments);//' The ' is context} ofn.apply (this, arguments); if (AFTERFN) {afterfn.apply (this, arguments);//' The ' is context} if (AROUNDFN) {aroundfn.apply (this, arguments);}}; }; return function (opt) {if (opt && typeof opt = = "Object" &&!opt.length) {options = opt; if (options.targ ET && checkcontext ()) {if (Options.before && typeof options.before = = "function") {Beforefn = options.b Efore; } if (Options.after && typeof options.after = = "function") {AfteRFN = Options.after; } if (Options.around && typeof options.after = = "function") {AROUNDFN = Options.around;} run (); } } }; })(); Test examples//-----------------AOP Modify global function---------------//function test (name, age) {Console.log ( ' Test fn. name = ' + name + ' Age: ' + age ';} AOP ({target: "Test", Before:function () {console.log ("AOP before");}, After:function () {Console.log ("AOP after"); Around:function () {console.log ("AOP Around");}); Run Test ("Adam", 6); -----------------AOP Test Modify method in an object---------------//var myobj = {myname: "testname", Sayname:func tion () {console.log (this.myname);}, Childobj: {age:6, say:function () {console.log (this.age);}}}; AOP ({context:myobj, target: "Sayname", Before:function () {console.log ("AOP before say name =" + This.myname);}, Afte R:function () {Console.log ("AOP after say name =" + This.myname);}, Around:function () {console.log ("AOP around say NA me = "+ This.mynaME); } }); Run Myobj.sayname (); AOP ({CONTEXT:MYOBJ.CHILDOBJ, target: "Say", Before:function () {console.log ("AOP before say name =" + This.age); Ter:function () {Console.log ("AOP after say name =" + This.age);}, Around:function () {console.log ("AOP around say Nam E = "+ This.age);}"; Myobj.childObj.say (); |