Function.js 1.2 KB

12345678910111213141516171819202122232425
  1. load("test-common.js");
  2. try {
  3. assert(Function.length === 1);
  4. assert(Function.prototype.length === 0);
  5. assert(typeof Function() === "function");
  6. assert(typeof new Function() === "function");
  7. assert(Function()() === undefined);
  8. assert(new Function()() === undefined);
  9. assert(Function("return 42")() === 42);
  10. assert(new Function("return 42")() === 42);
  11. assert(new Function("foo", "return foo")(42) === 42);
  12. assert(new Function("foo,bar", "return foo + bar")(1, 2) === 3);
  13. assert(new Function("foo", "bar", "return foo + bar")(1, 2) === 3);
  14. assert(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3) === 6);
  15. assert(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3) === 6);
  16. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true) === 42);
  17. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false) === "bar");
  18. assert(new Function("return typeof Function()")() === "function");
  19. assert(new Function("x", "return function (y) { return x + y };")(1)(2) === 3);
  20. console.log("PASS");
  21. } catch (e) {
  22. console.log("FAIL: " + e.message);
  23. }