本文實例講述了JavaScript AOP編程。分享給大家供大家參考。具體如下:
? 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 43 44 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 84 85 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 119 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: [email protected] // Version: 1.0 // Simple aspect oriented programming module // support Aspect before, after and around // usage: aop({ context: myObject, // scope context of the target function. target: "test", // target function name before: function() { // before function will be run before the target function console.log("aop before"); }, after: function() { // after function will be run after the target function console.log("aop after"); }, around: function() { // around function will be 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).name] === "function") { targetFnSelector = (options.target).name; targetFn = context[targetFnSelector]; } else if (typeof context[options.target] === "function") { targetFnSelector = options.target; targetFn = context[targetFnSelector]; } if (targetFn) { oFn = cloneFn(targetFn); oFnArg = new Array(targetFn.length); return true; } else { return false; } }, run = function() { context[targetFnSelector] = function(oFnArg) { if (aroundFn){ aroundFn.apply(this, arguments); } if (beforeFn){ beforeFn.apply(this, arguments); // 'this' is context } oFn.apply(this, arguments); if (afterFn){ afterFn.apply(this, arguments); // 'this' is context } if (aroundFn){ aroundFn.apply(this, arguments); } }; }; return function(opt){ if (opt && typeof opt === "object" && !opt.length) { options = opt; if (options.target && checkContext()) { if (options.before && typeof options.before === "function") { beforeFn = options.before; } 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: function() { 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); }, after: function() { console.log("aop after say name = " + this.myName); }, around: function() { console.log("aop around say name = " + this.myName); } }); // run myobj.sayName(); aop({ context: myobj.childObj, target: "say", before: function() { console.log("aop before say name = " + this.age);