You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB

  1. ---
  2. layout : 'default'
  3. css : 'javascript'
  4. title : 'Javascript'
  5. description : 'Suggestions for Javascript development practices'
  6. type : 'UI Guide'
  7. ---
  8. <%- @partial('header') %>
  9. <div class="main container">
  10. <div class="peek">
  11. <div class="ui vertical pointing secondary menu">
  12. <a class="active item">General</a>
  13. <a class="item">Optional</a>
  14. </div>
  15. </div>
  16. <h2 class="ui dividing header">General</h2>
  17. <p>Airbnb has compiled a wonderful list for a <em>mostly reasonable</em> approach to javascript. This is an excellent starting point for community consensus on javascript standards.</p>
  18. <p><a href="https://github.com/airbnb/javascript">https://github.com/airbnb/javascript</a></p>
  19. <h2 class="ui dividing header">Optional Considerations</h2>
  20. <h3 class="ui header">Variable Alignment</h4>
  21. <p>Matching equal signs increases code legibility but may take more time. There is a great plugin for Sublime Text called <a href="http://wbond.net/sublime_packages/alignment">Alignment</a> which can automatically manage this for you.</p>
  22. <div class="code" data-type="javascript">
  23. var
  24. dog = 'Poodle',
  25. cat = 'Cat',
  26. elephant = 'A long name'
  27. ;
  28. </div>
  29. <h3 class="ui header">Chaining</h4>
  30. <p>Indent chained code to show changes in the original selector. Use the ending semicolon as you would a closing bracket to show the original indentation level of the rule</p>
  31. <div class="code" data-type="javascript">
  32. $element
  33. .find('.one')
  34. .doSomething()
  35. .end()
  36. .find('.two')
  37. .doSomethingElse()
  38. ;
  39. </div>
  40. <h3 class="ui header">Verbs</h4>
  41. <p>When creating javascript modules consider using verbs to show behavior. This may be more intuitive than allowing a user to directly set properties or manage your internal namespace.</p>
  42. <div class="code" data-type="javascript">
  43. // this might require some more times with docs
  44. $element
  45. .widget('states.select.set')
  46. ;
  47. // reduce complexity to api
  48. $(element)
  49. .widget('activate', true)
  50. ;
  51. </div>
  52. <h3 class="ui header">Default Values</h4>
  53. <p>Using an OR value allows you to set defaults for any falsey value</p>
  54. <div class="code" data-type="javascript">
  55. (function(someBoolean, name) {
  56. var
  57. // this will have issues (false will evaluate same as undefined)
  58. someBoolean = someBoolean || true,
  59. // default name will be sally
  60. name = name || 'Sally'
  61. ;
  62. if(someBoolean) {
  63. alert(name);
  64. }
  65. }());
  66. </div>
  67. <h3 class="ui header">Grouping Variables</h4>
  68. <div class="code" data-type="javascript">
  69. // ok
  70. var
  71. offsetX = 2,
  72. offsetY = 5
  73. ;
  74. // nice
  75. var
  76. offset = {
  77. x: 2,
  78. y: 5
  79. }
  80. ;
  81. </div>
  82. </div>