Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="ES6 Spread and Rest">
  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. // Arrow functions
  13.  
  14. // normal function:
  15.  
  16. //function myFunc() {
  17. //}
  18.  
  19. // equivalent arrow function
  20.  
  21. //const myFunc = () => {
  22. //}
  23.  
  24. 'use strict';
  25.  
  26. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  27.  
  28. function printMyName(name) {
  29. console.log(name);
  30. }
  31.  
  32. printMyName('Simon');
  33.  
  34. var printMyNameArrow = function printMyNameArrow(name) {
  35. console.log(name);
  36. };
  37.  
  38. printMyNameArrow('Arrow function');
  39.  
  40. //if only one argument
  41.  
  42. var printMyNameArrow1 = function printMyNameArrow1(name) {
  43. console.log(name);
  44. };
  45.  
  46. printMyNameArrow1('shorthand Arrow function with only one argument');
  47.  
  48. // more than one argument
  49.  
  50. var printMyNameArrow2 = function printMyNameArrow2(name, age) {
  51. console.log(name, age);
  52. };
  53.  
  54. printMyNameArrow2('Simon', 21);
  55.  
  56. //if we want to just return something
  57.  
  58. var multiply = function multiply(number) {
  59. return number * 2;
  60. };
  61.  
  62. console.log('multiply: ' + multiply(2));
  63.  
  64. //shorthand
  65.  
  66. var multiplyShort = function multiplyShort(number) {
  67. return number * 2;
  68. };
  69.  
  70. console.log('multiplyShort: ' + multiplyShort(2));
  71.  
  72. //Spread
  73.  
  74. var numbers = [1, 2, 3];
  75.  
  76. //add additional element to numbers array
  77.  
  78. var newNumbers = [].concat(numbers, [4]);
  79.  
  80. console.log(newNumbers);
  81.  
  82. // [1, 2, 3, 4]
  83.  
  84. var person = {
  85. name: 'Max'
  86. };
  87.  
  88. // add a new property to the person object
  89. var newPerson = _extends({}, person, {
  90. age: 28
  91. });
  92.  
  93. console.log(newPerson);
  94.  
  95. //[object Object] {
  96. // age: 28,
  97. // name: "Max"
  98. //}
  99.  
  100. //Rest
  101.  
  102. var filter = function filter() {
  103. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  104. args[_key] = arguments[_key];
  105. }
  106.  
  107. return args.filter(function (el) {
  108. return el === 2;
  109. });
  110. };
  111. // === checks for type and value so must be a number and 2
  112. // filter expects true or false
  113. //
  114.  
  115. // creates an array from 'args'
  116.  
  117. console.log(filter(1, 2, 3));
  118.  
  119. // filter function in standard Js with no arrow function
  120.  
  121. var filterStandardJs = function filterStandardJs() {
  122. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  123. args[_key2] = arguments[_key2];
  124. }
  125.  
  126. return args.filter(function el() {
  127. return el;
  128. });
  129. };
  130.  
  131. console.log("filterStandardJs: " + filterStandardJs(1, 2, 3));
  132.  
  133. // applys a method to args to filter the required element from the array
  134.  
  135. //Destructuring
  136.  
  137. // array destructuring
  138. num1 = numbers[0];
  139. num2 = numbers[1];
  140.  
  141. console.log(num1, num2);
  142.  
  143. //pulls num1 and num2 out of 'numbers' array defined at top but not num3
  144. </script>
  145.  
  146.  
  147.  
  148. <script id="jsbin-source-javascript" type="text/javascript">
  149. // Arrow functions
  150.  
  151.  
  152. // normal function:
  153.  
  154. //function myFunc() {
  155. //}
  156.  
  157. // equivalent arrow function
  158.  
  159. //const myFunc = () => {
  160. //}
  161.  
  162. function printMyName(name){
  163. console.log(name);
  164. }
  165.  
  166. printMyName('Simon');
  167.  
  168.  
  169. const printMyNameArrow = (name) => {
  170. console.log(name);
  171. }
  172.  
  173. printMyNameArrow('Arrow function')
  174.  
  175.  
  176. //if only one argument
  177.  
  178. const printMyNameArrow1 = name => {
  179. console.log(name);
  180. }
  181.  
  182. printMyNameArrow1('shorthand Arrow function with only one argument')
  183.  
  184.  
  185. // more than one argument
  186.  
  187. const printMyNameArrow2 = (name, age) => {
  188. console.log(name, age);
  189. }
  190.  
  191. printMyNameArrow2('Simon', 21)
  192.  
  193. //if we want to just return something
  194.  
  195. const multiply = (number) => {
  196. return number * 2;
  197. }
  198.  
  199. console.log('multiply: ' + multiply(2));
  200.  
  201.  
  202. //shorthand
  203.  
  204. const multiplyShort = number => number *2;
  205.  
  206.  
  207. console.log('multiplyShort: ' + multiplyShort(2));
  208.  
  209.  
  210.  
  211.  
  212.  
  213. //Spread
  214.  
  215. const numbers = [1,2,3];
  216.  
  217. //add additional element to numbers array
  218.  
  219. const newNumbers = [...numbers, 4]
  220.  
  221. console.log(newNumbers);
  222.  
  223. // [1, 2, 3, 4]
  224.  
  225.  
  226.  
  227. const person = {
  228. name: 'Max'
  229. };
  230.  
  231.  
  232. // add a new property to the person object
  233. const newPerson = {
  234. ...person,
  235. age: 28
  236. }
  237.  
  238. console.log(newPerson);
  239.  
  240. //[object Object] {
  241. // age: 28,
  242. // name: "Max"
  243. //}
  244.  
  245.  
  246.  
  247. //Rest
  248.  
  249. const filter = (...args) => {
  250. return args.filter(el => el === 2);
  251. }
  252. // === checks for type and value so must be a number and 2
  253. // filter expects true or false
  254. //
  255.  
  256. // creates an array from 'args'
  257.  
  258. console.log(filter(1,2,3));
  259.  
  260.  
  261. // filter function in standard Js with no arrow function
  262.  
  263. const filterStandardJs = (...args) => {
  264. return args.filter(function el(){
  265. return el;
  266. });
  267. }
  268.  
  269. console.log("filterStandardJs: " + filterStandardJs(1,2,3));
  270.  
  271.  
  272.  
  273.  
  274. // applys a method to args to filter the required element from the array
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. //Destructuring
  282.  
  283. // array destructuring
  284. [num1, num2] = numbers;
  285.  
  286. console.log(num1, num2)
  287.  
  288. //pulls num1 and num2 out of 'numbers' array defined at top but not num3
  289.  
  290.  
  291.  
  292.  
  293. </script></body>
  294. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement