apply.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 write(v) { WScript.Echo(v + ""); }
  6. function f1()
  7. {
  8. this.x1 = "hello";
  9. }
  10. f1.apply();
  11. write("x1 : " + x1);
  12. x1 = 0;
  13. f1.apply(null);
  14. write("x1 : " + x1);
  15. x1 = 0;
  16. f1.apply(undefined);
  17. write("x1 : " + x1);
  18. var o = new Object();
  19. x1 = 0;
  20. f1.apply(o);
  21. write("x1 : " + x1);
  22. write("o.x1 : " + o.x1);
  23. function f2(a)
  24. {
  25. this.x2 = a;
  26. }
  27. x2 = 0;
  28. f2.apply();
  29. write("x2 : " + x2);
  30. x2 = 0;
  31. f2.apply(null);
  32. write("x2 : " + x2);
  33. x2 = 0;
  34. f2.apply(undefined);
  35. write("x2 : " + x2);
  36. x2 = 0;
  37. f2.apply(o);
  38. write("x2 : " + x2);
  39. write("o.x2 : " + o.x2);
  40. x2 = 0;
  41. f2.apply(null, ["world"]);
  42. write("x2 : " + x2);
  43. x2 = 0;
  44. f2.apply(undefined, ["world"]);
  45. write("x2 : " + x2);
  46. x2 = 0;
  47. f2.apply(o, ["world"]);
  48. write("x2 : " + x2);
  49. write("o.x2 : " + o.x2);
  50. function blah()
  51. {
  52. this.construct.apply(this, arguments);
  53. write("after apply");
  54. return new Object();
  55. }
  56. function blah2()
  57. {
  58. try
  59. {
  60. this.construct.apply(this, arguments);
  61. }
  62. catch (e)
  63. {
  64. }
  65. write("after apply");
  66. return new Object();
  67. }
  68. blah.prototype.construct = function(x, y)
  69. {
  70. this.a = x;
  71. this.b = y;
  72. }
  73. blah2.prototype.construct = function(x, y)
  74. {
  75. this.a = x;
  76. this.b = y;
  77. }
  78. var o = new blah(1, 2);
  79. write(o.a);
  80. write(o.b);
  81. o = new blah2(1, 2);
  82. write(o.a);
  83. write(o.b);
  84. function f() {}
  85. f.apply({},{});
  86. f.apply({},{length:null});
  87. f.apply({},{length:undefined});
  88. f.apply({},{length:0.5});