Function.prototype.call.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. load("test-common.js");
  2. try {
  3. function Foo(arg) {
  4. this.foo = arg;
  5. }
  6. function Bar(arg) {
  7. this.bar = arg;
  8. }
  9. function FooBar(arg) {
  10. Foo.call(this, arg);
  11. Bar.call(this, arg);
  12. }
  13. function FooBarBaz(arg) {
  14. Foo.call(this, arg);
  15. Bar.call(this, arg);
  16. this.baz = arg;
  17. }
  18. assert(Function.prototype.call.length === 1);
  19. var foo = new Foo("test");
  20. assert(foo.foo === "test");
  21. assert(foo.bar === undefined);
  22. assert(foo.baz === undefined);
  23. var bar = new Bar("test");
  24. assert(bar.foo === undefined);
  25. assert(bar.bar === "test");
  26. assert(bar.baz === undefined);
  27. var foobar = new FooBar("test");
  28. assert(foobar.foo === "test");
  29. assert(foobar.bar === "test");
  30. assert(foobar.baz === undefined);
  31. var foobarbaz = new FooBarBaz("test");
  32. assert(foobarbaz.foo === "test");
  33. assert(foobarbaz.bar === "test");
  34. assert(foobarbaz.baz === "test");
  35. assert(Math.abs.call(null, -1) === 1);
  36. var add = (x, y) => x + y;
  37. assert(add.call(null, 1, 2) === 3);
  38. var multiply = function (x, y) { return x * y; };
  39. assert(multiply.call(null, 3, 4) === 12);
  40. assert((() => this).call("foo") === globalThis);
  41. console.log("PASS");
  42. } catch (e) {
  43. console.log("FAIL: " + e);
  44. }