apply.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function check(value, expected) {
  6. if (value !== expected) {
  7. throw new Error("Test failed");
  8. }
  9. }
  10. function f1()
  11. {
  12. this.x1 = "hello";
  13. }
  14. f1.apply();
  15. check(x1, "hello");
  16. x1 = 0;
  17. f1.apply(null);
  18. check(x1, "hello");
  19. x1 = 0;
  20. f1.apply(undefined);
  21. check(x1, "hello");
  22. var o = new Object();
  23. x1 = 0;
  24. f1.apply(o);
  25. check(x1, 0);
  26. check(o.x1, "hello");
  27. function f2(a)
  28. {
  29. this.x2 = a;
  30. }
  31. x2 = 0;
  32. f2.apply();
  33. check(x2, undefined);
  34. x2 = 0;
  35. f2.apply(null);
  36. check(x2, undefined);
  37. x2 = 0;
  38. f2.apply(undefined);
  39. check(x2, undefined);
  40. x2 = 0;
  41. f2.apply(o);
  42. check(x2, 0);
  43. check(o.x2, undefined);
  44. x2 = 0;
  45. f2.apply(null, ["world"]);
  46. check(x2, "world");
  47. x2 = 0;
  48. f2.apply(undefined, ["world"]);
  49. check(x2, "world");
  50. x2 = 0;
  51. f2.apply(o, ["world"]);
  52. check(x2, 0);
  53. check(o.x2, "world");
  54. function blah()
  55. {
  56. this.construct.apply(this, arguments);
  57. return new Object();
  58. }
  59. function blah2()
  60. {
  61. try
  62. {
  63. this.construct.apply(this, arguments);
  64. }
  65. catch (e)
  66. {
  67. }
  68. return new Object();
  69. }
  70. blah.prototype.construct = function(x, y)
  71. {
  72. this.a = x;
  73. this.b = y;
  74. }
  75. blah2.prototype.construct = function(x, y)
  76. {
  77. this.a = x;
  78. this.b = y;
  79. }
  80. var o = new blah(1, 2);
  81. check(o.a, undefined);
  82. check(o.b, undefined);
  83. o = new blah2(1, 2);
  84. check(o.a, undefined);
  85. check(o.b, undefined);
  86. function f() {}
  87. f.apply({},{});
  88. f.apply({},{length:null});
  89. f.apply({},{length:undefined});
  90. f.apply({},{length:0.5});
  91. print("pass");