Function.prototype.call.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. function assert(x) { if (!x) throw 1; }
  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. console.log("PASS");
  41. } catch (e) {
  42. console.log("FAIL: " + e);
  43. }