Singleton Pattern


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Example singleton
  3.  */
  4.  
  5.  
  6. Singleton = (function() {
  7.  
  8. return {
  9.  
  10.  
  11. /**
  12. * Create a new dom node
  13. *
  14. * @param {String} className add a class to the node
  15. * @param {String} nodeName the node to create
  16. *
  17. * @example
  18. * <code>
  19. * Singleton.create('foo', 'span');
  20. * </code>
  21. *
  22. * @return {HTML element} the new node
  23. */
  24.  
  25. create : function( className, nodeName ) {
  26. nodeName = nodeName || 'div';
  27. var elem = doc.createElement( nodeName );
  28. elem.className = className;
  29. return elem;
  30. },
  31.  
  32. /**
  33. * Appends a string to 'Hello '
  34. *
  35. * @param {String} str the string to append
  36. *
  37. * @example
  38. * <code>
  39. * Singleton.helloworld('world');
  40. * </code>
  41. *
  42. * @return {String} the modified string
  43. */
  44.  
  45. helloworld : function( str ) {
  46. output = 'Hello ' + str;
  47. return output;
  48. }
  49.  
  50. }
  51.  
  52. })();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.