Function.prototype.call.js 1.2 KB

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