Function.js 1.4 KB

123456789101112131415161718192021222324252627
  1. try {
  2. assert(Function.length === 1);
  3. assert(Function.prototype.length === 0);
  4. assert(typeof Function() === "function");
  5. assert(typeof new Function() === "function");
  6. assert(Function()() === undefined);
  7. assert(new Function()() === undefined);
  8. assert(Function("return 42")() === 42);
  9. assert(new Function("return 42")() === 42);
  10. assert(new Function("foo", "return foo")(42) === 42);
  11. assert(new Function("foo,bar", "return foo + bar")(1, 2) === 3);
  12. assert(new Function("foo", "bar", "return foo + bar")(1, 2) === 3);
  13. assert(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3) === 6);
  14. assert(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3) === 6);
  15. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true) === 42);
  16. assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false) === "bar");
  17. assert(new Function("return typeof Function()")() === "function");
  18. // FIXME: This is equivalent to
  19. // (function (x) { return function (y) { return x + y;} })(1)(2)
  20. // and should totally work, but both currently fail with
  21. // Uncaught exception: [ReferenceError]: 'x' not known
  22. // assert(new Function("x", "return function (y) { return x + y };")(1)(2) === 3);
  23. console.log("PASS");
  24. } catch (e) {
  25. console.log("FAIL: " + e.message);
  26. }