2Danke
ERLEDIGT
NEIN
NEIN
ANTWORTEN
3
3
ZUGRIFFE
1272
1272
EMPFEHLEN
-
30.10.09 10:21 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.724
- Blog-Einträge
- 29
Hallo,
hier mal ein kleines Beispiel wie einfach man mit Java Script ein bestehendes Objekt mit einem Stellvertreter Objekt (Proxy) wrappen kann um so zusätzliche Aktionen rund um den Methodenaufruf (davor, danach, oder gar anstatt) durchführen zu können.
Code javascript: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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JavaScript Proxy Example</title> <script> //Proxy Factory var proxyFactory = { createProxy: function(target, invocationHandler){ var proxy = {}; for (var key in target) { if (typeof target[key] == "function") { (function(key){ proxy[key] = function(){ return invocationHandler(proxy, target, key, arguments); }; })(key); } /* we copy only functions else { proxy[key] = target[key]; } */ } return proxy; } }; //Invocation Handler function handle(proxy, target, method, args){ console.log("calling: " + method + " with args: " + args); var result = target[method].apply(target, args); return result; }; //Service var service = { operation1: function(arg1, arg2){ return "arg1:" + arg1 + " arg2:" + arg2; }, operation2: function(arg1){ return "arg1:" + arg1; } }; var serviceProxy = proxyFactory.createProxy(service, handle); alert(serviceProxy.operation1("aaa", 3)); alert(serviceProxy.operation2("xxx")); </script> </head> <body> </body> </html>
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Moin Thomas,
Ich als "Programmier-Laie" kann oftmals garnicht nachvollziehen, was die kontroverse Betrachtungsweise von JS durch Profis verursacht...diverse Lektüre konnte mich da ein wenig erleuchten.
Ich bin halt von der Natur aus ein Handwerker, der nicht den Weg im Auge hat, sondern das Ziel.
Ich glaube, dein Beispiel ist eine gute Demonstration von gutem Handwerk
Deswegen mag ich JS
-
04.11.09 23:53 #3
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.724
- Blog-Einträge
- 29
Hallo,
danke für die Blumen
Ich habe mich früher nie viel mit JS beschäftigt.
Baue seit einiger Zeit an einer JS lastigen WebApp oder RIA (wie man heute dazu sagt) und bekomme deshalb öfters mal Gelegenheit die "alten" Tricks / Paradigmen aus anderen Sprachen in JS umsetzen zu können.
Bisher ging das ziemlich leicht von der Hand
Insbesondere mit guter IDE/Browser Unterstützung (FireBug, FireRainbow, Chrome JS Editor, Aptana Studio etc.) macht JS Entwicklung richtig Spaß 
Btw.:
Vor einiger Zeit gabs mal nen coolen Vortrag zu JavaScript:
http://www.java-forum-stuttgart.de/folien/C7.pdf
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
22.02.11 01:27 #4
Hallo,
habe gerade gesehen, dass dieser Beitrag wohl 2 Mal vorhanden ist. Also auch hier der Hinweis auf den Fehler
:
http://www.tutorials.de/content/866-...avascript.html
Viele Grüße
Konstantin
Ähnliche Themen
-
RMI: dynamic proxys
Von retn im Forum JavaAntworten: 0Letzter Beitrag: 01.02.08, 19:04






Zitieren
Login




