Guest User

Untitled

a guest
Sep 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. /*jshint undef:true, forin:false, eqeqeq:true, browser:true, newcap:true */
  2. /*global console:true, $:true */
  3. (function (global) { // We always pass global here
  4.  
  5. // Note: at the top of the file, we define things we are going to use more
  6. // often in this file, like you would with CommonJS require or Python imports
  7. var bm = global.bm,
  8. _ = bm.util,
  9.  
  10. EventEmitter = global.EventEmitter2,
  11. AnotherClass = bm.AnotherClass;
  12.  
  13. // General stuff
  14. // -------------
  15. // - softtabs, 2 spaces
  16. // - 2 whitelines between each function/method
  17. // - jshint (see header) passing, always.
  18. // - Lines are <= 80char
  19.  
  20.  
  21. // utilFunctionA
  22. // -------------
  23. // Describe this here shortly. Note: util function that don't fit
  24. // in a class method, we put in here.
  25. function utilFunctionA() {
  26. // ......
  27. }
  28.  
  29.  
  30. // MyClass < BaseClass
  31. // -------------------
  32. // Brief description of what this thing does.
  33. // This, for example, takes a string and a Rainbow
  34. // and makes an instance of a Unicorn
  35. //
  36. // The < in the title means that MyClass inherits from BaseClass
  37. //
  38. // + params
  39. // - someString (String) - a string with the unicorns name
  40. // - rainbow (Rainbow) - a Rainbow object
  41. // + returns
  42. // - (Unicorn) - returns a new unicorn instance
  43.  
  44. function MyClass(name, rainbow) {
  45. // All variables are at the top of the function.
  46. var obj,
  47. elements,
  48. something,
  49. otherThingFromElement, // Rather a bit too verbose, than too short.
  50. value,
  51. x, l, el; // Unless it is really obvious
  52.  
  53. // Initialize all properties inside the constructor, even when null
  54. this.counter = 0;
  55. this.title = null;
  56.  
  57. // In case you want to be sure that all methods are always executed within
  58. // the scope of this instance, we do a bindAll. This is typically needed
  59. // when directly using instance methods as event handlers of DOM elements.
  60. _.bindAll(this);
  61.  
  62. // Do stuff
  63. l = elements.length;
  64. for (x = 0; x < l; x++) {
  65. el = elements[x];
  66. if (something === el) { // Always triple ===
  67.  
  68. // When something does not fit within 79 char, we break and
  69. // give a *double* indent, Python style.
  70. otherThingFromElement =
  71. something.getClassesTheFastestWayPossible(true, false, true);
  72.  
  73. // We are not afraid of comments.
  74. value = x > 0 ? "a" : "b";
  75. }
  76. }
  77.  
  78. if (true) {
  79. // ...
  80. } else if (true) {
  81. // ...
  82. } else {
  83. // ...
  84. }
  85.  
  86. // When short and readable, chain on a single line
  87. $("body").remove().end().find();
  88.  
  89. // When long or less readable, we can indent like this
  90. $("foo")
  91. .dsadas()
  92. .dasdsadas()
  93. .adasdasdas()
  94. .dasdasdas();
  95. }
  96.  
  97. // Inheritance
  98. _.inherits(MyClass, AnotherClass);
  99.  
  100.  
  101. // ### Public MyClass :: myMethod
  102. MyClass.prototype.myMethod = function(el) {
  103. // Often it is easy and clean to pass event handler like this
  104. $(el).click(this.handleSaveButton);
  105. };
  106.  
  107.  
  108. // ### Public MyClass :: myOtherMethod
  109. MyClass.prototype.myOtherMethod = function(el) {
  110. var self = this;
  111.  
  112. // But sometimes you might want to use self, and that is fine too,
  113. // as long as we choose the most readable option
  114. $(el).click(function() {
  115. $(this).removeClass('never-touched');
  116. self._updateInternalCounter();
  117. });
  118. };
  119.  
  120.  
  121. // ### Public MyClass :: handleSaveButton
  122. // When a method is an event handler, we name methods like "handle<Something".
  123. // This allows to directly el.click(this.handleSaveButton);
  124. MyClass.prototype.handleSaveButton = function(e) {
  125. e.preventDefault();
  126. };
  127.  
  128.  
  129. // ### Private MyClass :: updateInternalCounter
  130. // We mark methods as private with _. Methods are marked private only when they
  131. // are really obviously private and default to public, to prevent having those
  132. // underscores all over the place.
  133. MyClass.prototype._updateInternalCounter = function() {
  134. this.counter++; // We are perfectly fine with ++
  135. };
  136.  
  137.  
  138. })(this); // Always pass this as global
Add Comment
Please, Sign In to add comment