Posted By

eryoav on 11/12/11


Tagged

DesignPatterns


Versions (?)

singleton


 / Published in: JavaScript
 

  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <script>
  9. var SingnleTon =(function() {
  10.  
  11. var instanse;
  12.  
  13. function init() {
  14.  
  15. //do heavy process
  16.  
  17. return {
  18. hello : function() {
  19. console.log("Hello");
  20. }
  21. }
  22. }
  23.  
  24. return {
  25. getInstance : function() {
  26. if (!instanse) {
  27. instanse = init();
  28. }
  29. return instanse;
  30. }
  31.  
  32. }
  33. })();
  34.  
  35. var s = SingnleTon.getInstance().hello();
  36. </script>
  37. </body>
  38. </html>

Report this snippet  

You need to login to post a comment.