With2.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 X()
  6. {
  7. this.x = 1;
  8. }
  9. function Y(s)
  10. {
  11. this[s] = 2;
  12. }
  13. function Z()
  14. {
  15. this.z = 3;
  16. }
  17. Y.prototype = new Z();
  18. X.prototype = new Y("y");
  19. var x = new X();
  20. var y = new Y("yy");
  21. var z = new Z();
  22. with(x)
  23. {
  24. WScript.Echo("x.x = " + x);
  25. WScript.Echo("x.y = " + y);
  26. WScript.Echo("x.z = " + z);
  27. ++z;
  28. WScript.Echo("x.z = " + z);
  29. // refers to x.y
  30. with(y)
  31. {
  32. WScript.Echo("x.x = " + x);
  33. WScript.Echo("x.y = " + y);
  34. WScript.Echo("x.z = " + z);
  35. }
  36. y = new Object();
  37. y.m = 7;
  38. // refers to x.y
  39. with(y)
  40. {
  41. WScript.Echo("x.y.m = " + m);
  42. }
  43. y = undefined;
  44. if(y == undefined)
  45. {
  46. WScript.Echo("OK: y in with scope is undefined");
  47. }
  48. Z.prototype.zz = 1;
  49. WScript.Echo("x.zz = " + zz);
  50. // get rid of x.x
  51. x = undefined;
  52. if(x == undefined)
  53. {
  54. WScript.Echo("OK: x in with scope is undefined");
  55. }
  56. }
  57. with(Z.prototype)
  58. {
  59. zz *= 10;
  60. with(Z)
  61. {
  62. prototype.zz++;
  63. with(prototype)
  64. {
  65. zz *= 100;
  66. }
  67. }
  68. }
  69. var q = new Y("a");
  70. with(x)
  71. {
  72. WScript.Echo("x.x = " + x);
  73. WScript.Echo("x.y = " + y);
  74. WScript.Echo("x.z = " + z);
  75. WScript.Echo("x.zz = " + zz);
  76. }
  77. with(q) { with(q) { with(q) {
  78. WScript.Echo("q.a = " + a);
  79. WScript.Echo("q.zz = " + zz);
  80. }}}
  81. (function () {
  82. function a()
  83. {
  84. WScript.Echo("a is called");
  85. }
  86. (function(){
  87. try {
  88. throw a;
  89. }
  90. catch(x) {
  91. with({}){
  92. x();
  93. }
  94. }
  95. })();
  96. })();
  97. (function () {
  98. var o = {};
  99. var y = function x(){
  100. with(o){
  101. x(o.x = function(){});
  102. }
  103. };
  104. y();
  105. })();