1 1. <script>
2 2.
3 3. function Test(){
4 4. this.say1 = function(s){
5 5. alert(s);
6 6. }
7 7. this.say2 = function(s){
8 8. alert(s);
9 9. }
10 10. }
11 11.
12 12. function actsAsAspect(object) {
13 13. object.yield = null;
14 14. object.rv = { };
15 15. object.before = function(method, f) {
16 16. var original = eval("this." + method);
17 17. this[method] = function() {
18 18. f.apply(this, arguments);
19 19. return original.apply(this, arguments);
20 20. };
21 21. };
22 22. object.after = function(method, f) {
23 23. var original = eval("this." + method);
24 24. this[method] = function() {
25 25. this.rv[method] = original.apply(this, arguments);
26 26. return f.apply(this, arguments);
27 27. }
28 28. };
29 29. object.around = function(method, f) {
30 30. var original = eval("this." + method);
31 31. this[method] = function() {
32 32. this.yield = original;
33 33. return f.apply(this, arguments);
34 34. }
35 35. };
36 36. }
37 37.
38 38. function beforeHander(s){
39 39. alert("aspect said:");
40 40. }
41 41. function afterHander(s){
42 42. alert("said by aspect");
43 43. }
44 44.
45 45. var t = new Test();
46 46. actsAsAspect(t);
47 47. t.before("say1",beforeHander);
48 48. t.after("say2",afterHander);
49 49.
50 50. test = function(){
51 51. t.say1("hello1");
52 52. t.say2("hello2");
53 53. }
54 54. test();
55 55. </script>