object-method-shorthand.js 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. test("basic method shorthand", () => {
  2. const o = {
  3. foo: "bar",
  4. getFoo() {
  5. return this.foo;
  6. },
  7. };
  8. expect(o.getFoo()).toBe("bar");
  9. });
  10. test("numeric literal method shorthand", () => {
  11. const o = {
  12. foo: "bar",
  13. 12() {
  14. return this.foo;
  15. },
  16. };
  17. expect(o[12]()).toBe("bar");
  18. });
  19. test("string literal method shorthand", () => {
  20. const o = {
  21. foo: "bar",
  22. "hello friends"() {
  23. return this.foo;
  24. },
  25. };
  26. expect(o["hello friends"]()).toBe("bar");
  27. });
  28. test("computed property method shorthand", () => {
  29. const o = {
  30. foo: "bar",
  31. [4 + 10]() {
  32. return this.foo;
  33. },
  34. };
  35. expect(o[14]()).toBe("bar");
  36. });
  37. test("symbol computed property shorthand", () => {
  38. const s = Symbol("foo");
  39. const o = {
  40. foo: "bar",
  41. [s]() {
  42. return this.foo;
  43. },
  44. };
  45. expect(o[s]()).toBe("bar");
  46. });