for-scopes.js 799 B

12345678910111213141516171819202122232425262728293031323334
  1. test("var in for head", () => {
  2. for (var v = 5; false; );
  3. expect(v).toBe(5);
  4. });
  5. test("let in for head", () => {
  6. for (let l = 5; false; );
  7. expect(() => {
  8. l;
  9. }).toThrowWithMessage(ReferenceError, "'l' is not defined");
  10. });
  11. test("const in for head", () => {
  12. for (const c = 5; false; );
  13. expect(() => {
  14. c;
  15. }).toThrowWithMessage(ReferenceError, "'c' is not defined");
  16. });
  17. test("let variables captured by value", () => {
  18. let result = "";
  19. let functionList = [];
  20. for (let i = 0; i < 9; i++) {
  21. result += i;
  22. functionList.push(function () {
  23. return i;
  24. });
  25. }
  26. for (let i = 0; i < functionList.length; i++) {
  27. result += functionList[i]();
  28. }
  29. expect(result).toEqual("012345678012345678");
  30. });