本文实例讲述了JavaScript之AOP编程。分享给大家供大家参考。具体如下:
/* // aop({options}); // By: adamchow2326@yahoo.com.au // 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); }, after: function() { console.log("aop after say name = " + this.age); }, around: function() { console.log("aop around say name = " + this.age); } }); myobj.childObj.say();
希望本文所述对大家的javascript程序设计有所帮助。
标签:
JavaScript,AOP编程
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“JavaScript之AOP编程实例”评论...
更新动态
2024年11月13日
2024年11月13日
- 群星《电台情歌-凌晨时分》2CD[低速原抓WAV]
- 许巍《试音天碟》人声测试天碟[WAV分轨][1G]
- 蔡琴《你不要那样看着我的眼睛》SACD版[低速原抓WAV+CUE][1G]
- 费玉清《一剪梅》24K金碟德国版[低速原抓WAV+CUE][1G]
- 宝可梦大集结国服什么时候上线 大集结国服上线时间一览
- 宝可梦大集结国服官网地址是什么 大集结官方网址一览
- 宝可梦大集结开服5选1礼包怎么选 新手5选1宝可梦推荐
- 劳斯莱斯女车主丈夫坦言拒赔原因:确实有流量因素
- 《心灵杀手2》PS5 Pro实机演示:质量模式4K 30帧
- 玩家分享买二手盘暖心经历:盘上还有小贴纸表达感谢
- 殷秀梅.2014-沁园春·雪【太平洋影音】【WAV+CUE】
- 范玮琪.2003-真善美【福茂】【WAV+CUE】
- 陈雷.1995-烧翻卖【金圆唱片】【WAV+CUE】
- 乱石堆中一粒砂金——《使命召唤21》评测
- 【果娘聊天室】双11即将结束,各位今年买了啥?