Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="Lecture 8: (Fat) Arrow Functions">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>JS Bin</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. // EXPLORE (FAT) ARROW FUNCTIONS:
  13.  
  14. /*
  15. **************************************************************
  16. * *
  17. * OLD STYLE FUNCTION EXAMPLE: *
  18. * *
  19. * function fn() { *
  20. * *
  21. * console.log('Hello!'); *
  22. * *
  23. * } *
  24. * *
  25. * fn(); *
  26. * *
  27. **************************************************************
  28. */
  29.  
  30. //EQUIVALENT NEW STYLE OF FUNCTION - (FAT) ARROW FUNCTION:
  31.  
  32. /*
  33. var fn = () => {
  34.  
  35. console.log('\nHello!\n');
  36.  
  37. } // end fn()
  38. */
  39.  
  40. // NOTE: Above code commented out to allow for refactoring. When
  41. // function body is comprised of a single command, as in this
  42. // case, there is an equivalent shorthand that dispenses with
  43. // the curly braces, as shown below:
  44.  
  45. // The results should be identical.
  46.  
  47. // var fn = () => console.log('\nHello!\n');
  48.  
  49. // NOTE: Commented out once again for refactoring. Since our code
  50. // consists of a function body with only one command, and a known
  51. // property of all functions is that they may be used to return
  52. // a value the code may be refactored once again as follows:
  53.  
  54. // Call fn():
  55.  
  56. // fn(); // commented out for refactoring:
  57.  
  58. var fn = () => '\nHello!\n';
  59.  
  60. // Now just log the function itself to the console, with identical
  61. // results:
  62.  
  63. console.log( fn() );
  64.  
  65. // ANOTHER EXAMPLE OF SYNTAX EMPLOYED WHEN THERE ARE ARGUMENTS:
  66.  
  67. /*
  68. var fn2 = (a, b) => {
  69.  
  70. return a + b;
  71.  
  72. };
  73. */
  74.  
  75. // ABOVE COMMENTED OUT FOR THIS EQUIVALENT SHORTENED VERSION:
  76.  
  77. var fn2 = (a, b) => a + b;
  78.  
  79. console.log( fn2(5, 6), '\n' );
  80.  
  81.  
  82. // ANOTHER VARIANT SYNTAX With A SINGLE ARGUMENT:
  83.  
  84. // var fn3 = (a) => a + 5;
  85.  
  86. // NOTE: Above syntax commented out in favor of shortest possible syntax. When
  87. // there is only one argument, the parentheses may be omitted altogether as in
  88. // the following refactoring:
  89.  
  90. var fn3 = a => a + 5;
  91.  
  92. console.log( fn3(3), '\n');
  93. </script>
  94.  
  95.  
  96.  
  97. <script id="jsbin-source-javascript" type="text/javascript">// EXPLORE (FAT) ARROW FUNCTIONS:
  98.  
  99. /*
  100. **************************************************************
  101. * *
  102. * OLD STYLE FUNCTION EXAMPLE: *
  103. * *
  104. * function fn() { *
  105. * *
  106. * console.log('Hello!'); *
  107. * *
  108. * } *
  109. * *
  110. * fn(); *
  111. * *
  112. **************************************************************
  113. */
  114.  
  115. //EQUIVALENT NEW STYLE OF FUNCTION - (FAT) ARROW FUNCTION:
  116.  
  117. /*
  118. var fn = () => {
  119.  
  120. console.log('\nHello!\n');
  121.  
  122. } // end fn()
  123. */
  124.  
  125. // NOTE: Above code commented out to allow for refactoring. When
  126. // function body is comprised of a single command, as in this
  127. // case, there is an equivalent shorthand that dispenses with
  128. // the curly braces, as shown below:
  129.  
  130. // The results should be identical.
  131.  
  132. // var fn = () => console.log('\nHello!\n');
  133.  
  134. // NOTE: Commented out once again for refactoring. Since our code
  135. // consists of a function body with only one command, and a known
  136. // property of all functions is that they may be used to return
  137. // a value the code may be refactored once again as follows:
  138.  
  139. // Call fn():
  140.  
  141. // fn(); // commented out for refactoring:
  142.  
  143. var fn = () => '\nHello!\n';
  144.  
  145. // Now just log the function itself to the console, with identical
  146. // results:
  147.  
  148. console.log( fn() );
  149.  
  150. // ANOTHER EXAMPLE OF SYNTAX EMPLOYED WHEN THERE ARE ARGUMENTS:
  151.  
  152. /*
  153. var fn2 = (a, b) => {
  154.  
  155. return a + b;
  156.  
  157. };
  158. */
  159.  
  160. // ABOVE COMMENTED OUT FOR THIS EQUIVALENT SHORTENED VERSION:
  161.  
  162. var fn2 = (a, b) => a + b;
  163.  
  164. console.log( fn2(5, 6), '\n' );
  165.  
  166.  
  167. // ANOTHER VARIANT SYNTAX With A SINGLE ARGUMENT:
  168.  
  169. // var fn3 = (a) => a + 5;
  170.  
  171. // NOTE: Above syntax commented out in favor of shortest possible syntax. When
  172. // there is only one argument, the parentheses may be omitted altogether as in
  173. // the following refactoring:
  174.  
  175. var fn3 = a => a + 5;
  176.  
  177. console.log( fn3(3), '\n');
  178. </script></body>
  179. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement