new-expression.js 929 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // prettier-ignore
  2. test("new-expression parsing", () => {
  3. function Foo() {
  4. this.x = 1;
  5. }
  6. let foo = new Foo();
  7. expect(foo.x).toBe(1);
  8. foo = new Foo
  9. expect(foo.x).toBe(1);
  10. foo = new
  11. Foo
  12. ();
  13. expect(foo.x).toBe(1);
  14. foo = new Foo + 2
  15. expect(foo).toBe("[object Object]2");
  16. });
  17. // prettier-ignore
  18. test("new-expressions with object keys", () => {
  19. let a = {
  20. b: function () {
  21. this.x = 2;
  22. },
  23. };
  24. foo = new a.b();
  25. expect(foo.x).toBe(2);
  26. foo = new a.b;
  27. expect(foo.x).toBe(2);
  28. foo = new
  29. a.b();
  30. expect(foo.x).toBe(2);
  31. });
  32. test("new-expressions with function calls", () => {
  33. function funcGetter() {
  34. return function (a, b) {
  35. this.x = a + b;
  36. };
  37. }
  38. foo = new funcGetter()(1, 5);
  39. expect(foo).toBeUndefined();
  40. foo = new (funcGetter())(1, 5);
  41. expect(foo.x).toBe(6);
  42. });