Guest User

Untitled

a guest
Jul 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. /* 1 - keep js separate */
  2.  
  3. // these are the same but #2 can be passed around.
  4. function myFunc(a) {
  5.  
  6. }
  7. var myFunc = function(a) {
  8.  
  9. }
  10.  
  11. // wire up events with observe
  12. var clicker = function(event) {
  13. var elm = Event.element(event);
  14. console.log(elm);
  15.  
  16. Event.stop(event);
  17. };
  18. $('linky').observe('click', clicker);
  19.  
  20. // or anonymous function if callback doesn't need to be re-used
  21. $('linky').observe('click', function(event) {
  22. var elm = Event.element(event);
  23.  
  24. Event.stop(event);
  25. });
  26.  
  27. /* 2 - Maintain scope */
  28. // use json to group related functions / properties
  29. var Robot = {
  30. killCount: 0,
  31. kill: function() {
  32. alert('Affirmative');
  33. Robot.killCount++;
  34. }
  35. }
  36. Robot.kill();
  37. Robot.kill();
  38. alert('Hey Bro-bot I just got ' + Robot.killCount + ' kills. Affirmative.');
  39.  
  40. // limit scope with the self executing function
  41. (function() {
  42.  
  43. var clutterTheGlobalSpace = false;
  44. function doI(clutter) {
  45.  
  46. }
  47.  
  48. })();
  49.  
  50. /* Feature/object detect */
  51. // be defensive
  52. var elm = $('example');
  53. if (elm) {
  54. elm.hide();
  55. }
  56. if (elm) {
  57. var content = elm.textContent ? elm.textContent : elm.innerText;
  58. }
  59. function jiggle(elm, options) {
  60. options = options || {};
  61. var duration = options.duration || 1000;
  62. alert("I'm gonna jiggle for " + duration + " miliseconds.");
  63. }
  64.  
  65.  
  66. // 4 - Use literals to house the code
  67. var Util = {},
  68. Controls = {},
  69. Page = {
  70. launchNewWindow: function(url,options) {
  71.  
  72. }
  73. };
  74.  
  75. // use json for optional parameters
  76. var elm = $('example');
  77. jiggle(elm, {
  78. duration: 2000,
  79. size: 10,
  80. times: 4
  81. });
  82.  
  83. // 5 - Efficiency
  84. // safe references to prevent lookups
  85. function walkTheDom(elm) {
  86. elm = $(elm);
  87. var wrapper = elm.up('.wrapper'),
  88. content = elm.down('.content'),
  89. header = elm.down('.header');
  90. }
  91.  
  92. // the definitive for loop
  93. var numbers = [1,2,3,4,5,6,7,8,9,10];
  94. for (var i=0, ii=numbers.length; i<ii; i++) {
  95.  
  96. }
  97.  
  98. // create comma separated variables at the top of a block
  99. function dance() {
  100. var steps = [],
  101. timer = null,
  102. partners = [];
  103. }
Add Comment
Please, Sign In to add comment