arrow-functions.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. let chompy = [(x) => x, 2];
  15. assert(chompy.length === 2);
  16. assert(chompy[0](1) === 1);
  17. const makeObject = (a, b) => ({ a, b });
  18. const obj = makeObject(33, 44);
  19. assert(typeof obj === "object");
  20. assert(obj.a === 33);
  21. assert(obj.b === 44);
  22. let returnUndefined = () => { };
  23. assert(typeof returnUndefined() === "undefined");
  24. const makeArray = (a, b) => [a, b];
  25. const array = makeArray("3", { foo: 4 });
  26. assert(array[0] === "3");
  27. assert(array[1].foo === 4);
  28. let square = x => x * x;
  29. assert(square(3) === 9);
  30. let squareBlock = x => {
  31. return x * x;
  32. };
  33. assert(squareBlock(4) === 16);
  34. const message = (who => "Hello " + who)("friends!");
  35. assert(message === "Hello friends!");
  36. const sum = ((x, y, z) => x + y + z)(1, 2, 3);
  37. assert(sum === 6);
  38. const product = ((x, y, z) => {
  39. let res = x * y * z;
  40. return res;
  41. })(5, 4, 2);
  42. assert(product === 40);
  43. const half = (x => {
  44. return x / 2;
  45. })(10);
  46. assert(half === 5);
  47. var foo, bar;
  48. foo = bar, baz => {};
  49. assert(foo === undefined);
  50. assert(bar === undefined);
  51. function FooBar() {
  52. this.x = {
  53. y: () => this,
  54. z: function () {
  55. return (() => this)();
  56. }
  57. };
  58. }
  59. var foobar = new FooBar();
  60. assert(foobar.x.y() === foobar);
  61. assert(foobar.x.z() === foobar.x);
  62. var Baz = () => {};
  63. assert(Baz.prototype === undefined);
  64. assertThrowsError(() => {
  65. new Baz();
  66. }, {
  67. error: TypeError,
  68. message: "Baz is not a constructor"
  69. });
  70. (() => {
  71. "use strict";
  72. assert(isStrictMode());
  73. (() => {
  74. assert(isStrictMode());
  75. })();
  76. })();
  77. (() => {
  78. 'use strict';
  79. assert(isStrictMode());
  80. })();
  81. (() => {
  82. assert(!isStrictMode());
  83. (() => {
  84. "use strict";
  85. assert(isStrictMode());
  86. })();
  87. assert(!isStrictMode());
  88. })();
  89. console.log("PASS");
  90. } catch {
  91. console.log("FAIL");
  92. }