Reflect.construct.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. load("test-common.js");
  2. try {
  3. assert(Reflect.construct.length === 2);
  4. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  5. assertThrowsError(() => {
  6. Reflect.construct(value);
  7. }, {
  8. error: TypeError,
  9. message: "First argument of Reflect.construct() must be a function"
  10. });
  11. assertThrowsError(() => {
  12. Reflect.construct(() => {}, value);
  13. }, {
  14. error: TypeError,
  15. message: "Arguments list must be an object"
  16. });
  17. assertThrowsError(() => {
  18. Reflect.construct(() => {}, [], value);
  19. }, {
  20. error: TypeError,
  21. message: "Optional third argument of Reflect.construct() must be a constructor"
  22. });
  23. });
  24. var a = Reflect.construct(Array, [5]);
  25. assert(a instanceof Array);
  26. assert(a.length === 5);
  27. var s = Reflect.construct(String, [123]);
  28. assert(s instanceof String);
  29. assert(s.length === 3);
  30. assert(s.toString() === "123");
  31. function Foo() {
  32. this.name = "foo";
  33. }
  34. function Bar() {
  35. this.name = "bar";
  36. }
  37. var o = Reflect.construct(Foo, [], Bar);
  38. assert(o.name === "foo");
  39. assert(o instanceof Foo === false);
  40. assert(o instanceof Bar === true);
  41. console.log("PASS");
  42. } catch (e) {
  43. console.log("FAIL: " + e);
  44. }