Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Tabs or Spaces
  2. Tabs
  3.  
  4. ## Whitespace
  5. Whitespace is added in the following locations.
  6.  
  7. ```js
  8. //After keywords
  9. if (
  10.  
  11. //Before Brackets
  12. if (a) {
  13.  
  14. //Around mathematical operators
  15. var a = b + c;
  16.  
  17. //Around boolean operators
  18. if ((a) || (b))
  19. ```
  20.  
  21. ## Bracket Positioning
  22. ```js
  23. if (a) {
  24.  
  25. } else {
  26.    
  27. }
  28. ```
  29.  
  30. ## Single or Double Quotes
  31. Single.
  32. ```js
  33. var a = 'This is a string';
  34. ```
  35. In cases where a single quote is required in the string, use backticks:
  36. ```js
  37. var a = `This string contains 'single quotes'`;
  38. ```
  39.  
  40. ## String Concatenation
  41. To build a string up from variables, use template literals:
  42. ```js
  43. var a = `Name: ${name}`;
  44. ```
  45.  
  46. ## Conditionals
  47. Each conditional has brackets surrounding it:
  48. ```js
  49. if ((a) && (b == c))
  50. ```
  51.  
  52. In cases where there are more than one operator, new-lines and indentation is used to improve readability:
  53.  
  54. ```js
  55. if (
  56.     (a) &&
  57.     (
  58.         (b == c) ||
  59.         (b == d)
  60.     )
  61. )
  62. ```
  63.  
  64. When an if contains only one statement, no brackets are added:
  65. ```js
  66. if (a)
  67.     b();
  68. ```
  69.  
  70. ## For Loops
  71. The following iterators (in this order) are used: `i, j, k, l`.
  72.  
  73. Iterators are never pre-declared.
  74.  
  75. If the array terminator is a variable, it must be cached if it consists of more than one term:
  76. ```js
  77. //no
  78. for (var i = 0; i < objects.length; i++)
  79.  
  80. //yes
  81. var oLen = objects.length;
  82. for (var i = 0; i < oLen; i++)
  83. ```
  84.  
  85. No other statements may be part of the loop declaration. That is, none of this:
  86. ```js
  87. for (var i = 0; i++, j += 1; i < 10)
  88. ```
  89.  
  90. ## In-line Functions
  91. To manipulate/find items in arrays, arrow functions are used:
  92. ```js
  93. var exists = array.find(a => (a == b));
  94. ```
  95.  
  96. In cases where the match is calculated in more than one statement, regular functions are used:
  97. ```js
  98. var exists = array.find(function(a) {
  99.     var b = a * 2;
  100.     return (b == 4);
  101. });
  102. ```
  103.  
  104. When the in-line function is a callback that is meant to be called later, bound functions are used:
  105. ```js
  106. db.query(queryString, this.onQuery.bind(this);
  107. ```
  108.  
  109. ## Multi-Line Strings
  110. To build multi-line strings, use backticks and the proper indentation:
  111. ```js
  112. var a = `
  113.     This is
  114.     a multi-line
  115.     string.
  116. `;
  117. ```
  118.  
  119. ## JSON Objects
  120. JSON Objects are never declared on one line:
  121. ```js
  122. //wrong
  123. var a = { b: 1, c: 2 };
  124.  
  125. //right
  126. var a = {
  127.     b: 1,
  128.     c: 2
  129. };
  130. ```
  131.  
  132. ## Arrays
  133. Small arrays can be defined on one line if spaces are placed after and before the first and last square bracket:
  134. ```js
  135. var a = [ 1, 2, 3 ];
  136. ```
  137.  
  138. Bigger arrays must be defined on multiple lines:
  139. ```js
  140. var a = [
  141.     1,
  142.     2,
  143.     3,
  144.     4,
  145.     5
  146. ];
  147. ```
  148.  
  149. ## Comments
  150. Single line comments are written with no space after the slashes:
  151. ```js
  152. //Single line comment
  153. ```
  154.  
  155. Multi-line comments are written with spaces after the brackets on all but the first line.
  156. ```js
  157. //Multi-line
  158. // comment
  159. //Another multi-line
  160. // comment
  161. ```
  162.  
  163. ## Module Syntax
  164. Things to note:
  165. * Each included module it listen on its own line.
  166. * Gap after properties
  167. * Gap after each method
  168. * Space after `function()`
  169.  
  170. ```js
  171. define([
  172.     'a/a',
  173.     'a/b'
  174. ], function(
  175.     a,
  176.     b
  177. ) {
  178.     return {
  179.         propA: 1,
  180.  
  181.         methodA: function() {
  182.  
  183.         },
  184.  
  185.         methodB: function() {
  186.        
  187.         }
  188.     };
  189. });
  190. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement