麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

JavaScript之AOP編程實(shí)例

2024-05-06 16:23:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了JavaScript的AOP編程,以實(shí)例形式分析了javascript面向切面編程的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了JavaScript之AOP編程。分享給大家供大家參考。具體如下:

 

 
  1. /* 
  2. // aop({options}); 
  3. // By: [email protected] 
  4. // Version: 1.0 
  5. // Simple aspect oriented programming module 
  6. // support Aspect before, after and around 
  7. // usage: 
  8. aop({ 
  9. context: myObject, // scope context of the target function. 
  10. target: "test", // target function name 
  11. before: function() { // before function will be run before the target function 
  12. console.log("aop before"); 
  13. }, 
  14. after: function() { // after function will be run after the target function 
  15. console.log("aop after"); 
  16. }, 
  17. around: function() { // around function will be run before and after the target function 
  18. console.log("aop around"); 
  19. } 
  20. }); 
  21. */ 
  22. var aop = (function() { 
  23. var options = {}, 
  24. context = window, 
  25. oFn, 
  26. oFnArg, 
  27. targetFn, 
  28. targetFnSelector, 
  29. beforeFn, 
  30. afterFn, 
  31. aroundFn, 
  32. cloneFn = function(Fn) { 
  33. if (typeof Fn === "function") { 
  34. return eval('[' +Fn.toString()+ ']')[0]; 
  35. return null
  36. }, 
  37. checkContext = function() { 
  38. if (options.context) { 
  39. context = options.context; 
  40. if (typeof context[(options.target).name] === "function") { 
  41. targetFnSelector = (options.target).name; 
  42. targetFn = context[targetFnSelector]; 
  43. else if (typeof context[options.target] === "function") { 
  44. targetFnSelector = options.target; 
  45. targetFn = context[targetFnSelector]; 
  46. if (targetFn) { 
  47. oFn = cloneFn(targetFn); 
  48. oFnArg = new Array(targetFn.length); 
  49. return true
  50. else { 
  51. return false
  52. }, 
  53. run = function() { 
  54. context[targetFnSelector] = function(oFnArg) { 
  55. if (aroundFn){ 
  56. aroundFn.apply(this, arguments); 
  57. if (beforeFn){ 
  58. beforeFn.apply(this, arguments); // 'this' is context 
  59. oFn.apply(this, arguments); 
  60. if (afterFn){ 
  61. afterFn.apply(this, arguments); // 'this' is context 
  62. if (aroundFn){ 
  63. aroundFn.apply(this, arguments); 
  64. }; 
  65. }; 
  66. return function(opt){ 
  67. if (opt && typeof opt === "object" && !opt.length) { 
  68. options = opt; 
  69. if (options.target && checkContext()) { 
  70. if (options.before && typeof options.before === "function") { 
  71. beforeFn = options.before; 
  72. if (options.after && typeof options.after === "function") { 
  73. afterFn = options.after; 
  74. if (options.around && typeof options.after === "function") { 
  75. aroundFn = options.around; 
  76. run(); 
  77. }; 
  78. })(); 
  79. // test examples 
  80. // ----------------- aop modify global function ---------------// 
  81. function test(name, age) { 
  82. console.log("test fn. name = " + name + " age: " + age); 
  83. aop({ 
  84. target: "test"
  85. before: function() { 
  86. console.log("aop before"); 
  87. }, 
  88. after: function() { 
  89. console.log("aop after"); 
  90. }, 
  91. around: function() { 
  92. console.log("aop around"); 
  93. }); 
  94. // run 
  95. test("adam", 6); 
  96. // ----------------- aop test modify method in an object ---------------// 
  97. var myobj = { 
  98. myName: "testName"
  99. sayName: function() { 
  100. console.log(this.myName); 
  101. }, 
  102. childObj: { 
  103. age: 6, 
  104. say: function() { 
  105. console.log(this.age); 
  106. }; 
  107. aop({ 
  108. context: myobj, 
  109. target: "sayName"
  110. before: function() { 
  111. console.log("aop before say name = " + this.myName); 
  112. }, 
  113. after: function() { 
  114. console.log("aop after say name = " + this.myName); 
  115. }, 
  116. around: function() { 
  117. console.log("aop around say name = " + this.myName); 
  118. }); 
  119. // run 
  120. myobj.sayName(); 
  121. aop({ 
  122. context: myobj.childObj, 
  123. target: "say"
  124. before: function() { 
  125. console.log("aop before say name = " + this.age); 
  126. }, 
  127. after: function() { 
  128. console.log("aop after say name = " + this.age); 
  129. }, 
  130. around: function() { 
  131. console.log("aop around say name = " + this.age); 
  132. }); 
  133. myobj.childObj.say(); 

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 久久久久久久网站 | 亚洲字幕av| 成人午夜视频免费看 | 麻豆小视频在线观看 | 国产www免费| 91久久久久久久一区二区 | 97zyz成人免费视频 | 亚洲第一页综合 | 一区二区三区欧洲 | 免费a视频在线观看 | 激情宗合| 亚洲午夜天堂吃瓜在线 | 精品国产一区三区| 国产精品自拍99 | 国产三级在线视频观看 | 亚欧美一区二区 | 精品国产一区二区三区四区阿崩 | 爱逼爱操综合网 | 密室逃脱第一季免费观看完整在线 | 黄色的视频免费观看 | www亚洲免费 | 国产女厕一区二区三区在线视 | 免费观看国产视频 | 午夜精品久久久久久久爽 | 久久久久久久久久久久免费 | 久久精品国产清自在天天线 | 成人福利在线播放 | 成年免费视频黄网站在线观看 | 久久成人免费观看 | 精品久久久91 | 久久精品1区2区 | 成人啪啪色婷婷久 | 中文字幕在线观看视频一区 | www.99av | 日本在线视频二区 | 少妇一级淫片免费放播放 | 国产在线欧美日韩 | 狠狠干最新网址 | 老司机一级毛片 | 日韩在线播放第一页 | 国产外围在线 |