arrow-functions.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. load("test-common.js");
  2. try {
  3. let getNumber = () => 42;
  4. assert(getNumber() === 42);
  5. getNumber = () => 99;
  6. assert(getNumber() === 99);
  7. let add = (a, b) => a + b;
  8. assert(add(2, 3) === 5);
  9. const addBlock = (a, b) => {
  10. let res = a + b;
  11. return res;
  12. };
  13. assert(addBlock(5, 4) === 9);
  14. const makeObject = (a, b) => ({ a, b });
  15. const obj = makeObject(33, 44);
  16. assert(typeof obj === "object");
  17. assert(obj.a === 33);
  18. assert(obj.b === 44);
  19. let returnUndefined = () => { };
  20. assert(typeof returnUndefined() === "undefined");
  21. const makeArray = (a, b) => [a, b];
  22. const array = makeArray("3", { foo: 4 });
  23. assert(array[0] === "3");
  24. assert(array[1].foo === 4);
  25. let square = x => x * x;
  26. assert(square(3) === 9);
  27. let squareBlock = x => {
  28. return x * x;
  29. };
  30. assert(squareBlock(4) === 16);
  31. const message = (who => "Hello " + who)("friends!");
  32. assert(message === "Hello friends!");
  33. const sum = ((x, y, z) => x + y + z)(1, 2, 3);
  34. assert(sum === 6);
  35. const product = ((x, y, z) => {
  36. let res = x * y * z;
  37. return res;
  38. })(5, 4, 2);
  39. assert(product === 40);
  40. const half = (x => {
  41. return x / 2;
  42. })(10);
  43. assert(half === 5);
  44. var foo, bar;
  45. foo = bar, baz => {};
  46. assert(foo === undefined);
  47. assert(bar === undefined);
  48. console.log("PASS");
  49. } catch {
  50. console.log("FAIL");
  51. }