Javascript Class Template


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

jsHint happy class template for javascript


Copy this code and paste it in your HTML
  1. /*jshint browser:true, jquery:true, undef:true, devel:true, unused:false, curly:true, strict:true,
  2. noarg:true, noempty:true, eqeqeq:true, bitwise:true, quotmark:true, indent:2, maxerr:50 */
  3. /*global window:true */
  4.  
  5. /**
  6.  Global Class collection with a class(klass) object with private and public variables/methods without using this.
  7.  @author John Doe
  8.  @namespace classLib
  9. */
  10. (function (window) {
  11. 'use strict';
  12.  
  13. var classLib = window.classLib || {};
  14.  
  15. /**
  16.   @class klass
  17.   @namespace classLib
  18.   */
  19. classLib.klass = function () {
  20. /* Private variables */
  21. var _foo, _bar;
  22.  
  23. _foo = 'foo';
  24. _bar = 'bar';
  25. /*-------------------------------------------------------*/
  26. /* Private functions */
  27. var _privateFunc = function () {
  28. _foo = _bar;
  29. };
  30.  
  31. return {
  32. /* Public functions */
  33. init: function () {
  34. privateFunc();
  35. },
  36. getFoo: function () {
  37. return _foo;
  38. },
  39. setFoo: function (value) {
  40. _foo = value || _foo;
  41. }
  42. };
  43. }();
  44.  
  45. // Expose klass to the global object
  46. window.classLib = classLib;
  47.  
  48. }(window));

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.