code_junkie

What Are Some Examples of Design Pattern Implementations Using JavaScript?

Nov 10th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. var com = window.com || {};
  2. com.mynamespace = com.mynamespace || {};
  3. com.mynamespace.newpackage = (function() {
  4. var myPrivateVariable = "hidden";
  5. var myPublicVariable = "exposed";
  6.  
  7. function myPrivateMethod() {
  8. return "also hidden";
  9. }
  10.  
  11. function myPublicMethod() {
  12. return "also exposed";
  13. }
  14.  
  15. return {
  16. myPublicMethod: myPublicMethod,
  17. myPublicVariable: myPublicVariable
  18. };
  19. })();
  20.  
  21. # create a factory for MyObject
  22. my_factory = MyObject
  23. instance_1 = my_factory.new
  24.  
  25. # change the factory to another class
  26. my_factory = MyOtherObject
  27. instance_2 = my_factory.new
  28.  
  29. var singleton = {
  30. sayHello: function() {
  31. alert("Hello!")
  32. }
  33. };
  34.  
  35. // This object controls the progress of the animation
  36. ex1 = new Animator();
  37. // The Animator's subjects define its behaviour
  38. ex1.addSubject(updateButton);
  39. function updateButton(value) {
  40. $('ex1Button').innerHTML = "Progress: " + Math.round(value * 100) + "%";
  41. }
  42.  
  43. // now click below: each click to the button calls ex1.toggle()
Add Comment
Please, Sign In to add comment