Accessors.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. var o = new Object();
  6. WScript.Echo("*** Setting data property ***");
  7. o.x = 23;
  8. WScript.Echo("o.x=" + o.x);
  9. WScript.Echo("*** Setting data property using defineProperty ***");
  10. Object.defineProperty(o, "x", { value : 24 });
  11. WScript.Echo("o.x=" + o.x);
  12. WScript.Echo("*** Setting accessor property using defineProperty ***");
  13. var x = "";
  14. var z = "";
  15. Object.defineProperty(o, "x", {
  16. get : function() { WScript.Echo("Getter called"); return x; },
  17. set : function(val) { WScript.Echo("Setter called"); z = 1000; x = val; }
  18. });
  19. o.x = 25;
  20. WScript.Echo("o.x=" + o.x);
  21. WScript.Echo("x=" + x);
  22. WScript.Echo("z=" + z);
  23. WScript.Echo("*** Setting backing store for accessor ***");
  24. x = 26;
  25. WScript.Echo("o.x=" + o.x);
  26. var ab = new Object();
  27. Object.defineProperty(ab,"foo",{get:function(){WScript.Echo("In getter");}, configurable: true});
  28. Object.defineProperty(ab,"foo",{set:function(arg){WScript.Echo("In setter");}});
  29. ab.foo;
  30. ab.foo = 10;
  31. delete ab.foo;
  32. try
  33. {
  34. var ab = new Object();
  35. Object.defineProperty(ab,"foo",{get:function(){WScript.Echo("In getter");}});
  36. ab.foo;
  37. ab.foo = 10;
  38. }
  39. catch(e)
  40. {
  41. WScript.Echo(e.description);
  42. }
  43. delete ab.foo;
  44. try
  45. {
  46. var ab = new Object();
  47. Object.defineProperty(ab,"foo",{set:function(arg){WScript.Echo("In setter");}});
  48. WScript.Echo(ab.foo);
  49. ab.foo = 10;
  50. }
  51. catch(e)
  52. {
  53. WScript.Echo(e.description);
  54. }
  55. delete ab.foo;
  56. var o = {};
  57. o.a = 1;
  58. o.b = 2;
  59. o.c = 3;
  60. o.d = 4;
  61. o.e = 5;
  62. o.f = 6;
  63. o.g = 7;
  64. o.h = 8;
  65. o.i = 9;
  66. o.j = 10;
  67. o.k = 11;
  68. o.l = 12;
  69. o.m = 13;
  70. o.n = 14;
  71. o.o = 15;
  72. o.p = 16;
  73. o.q = 17;
  74. Object.defineProperty(o, "qqq",
  75. {
  76. set: function () { },
  77. get: function() { WScript.Echo("get"); }
  78. });
  79. WScript.Echo(o.qqq);
  80. delete o.qqq;
  81. // prototype setter/getter
  82. function Point() {
  83. this.x=0;
  84. this.y=0;
  85. }
  86. Point.prototype = {
  87. print:function() { WScript.Echo("x:"+this.x+", y:"+this.y+", z:"+this.z); }
  88. };
  89. Object.defineProperty(Point.prototype,"z",{ set:function(v) { this._z=v; }, get: function() { return this._z; }});
  90. var pt=new Point();
  91. pt.z=12;
  92. pt.print();
  93. Object.defineProperty(this, "abc",
  94. {
  95. set: function () { },
  96. get: function() { WScript.Echo("get global"); }
  97. });
  98. WScript.Echo(abc);
  99. delete this.abc;
  100. (function () {
  101. WScript.Echo("*** Getters, prototypes, and deleting properties ***");
  102. function A() { };
  103. A.prototype = {
  104. get p () { return this._p; },
  105. set p (v) { this._p = v; }
  106. };
  107. var o = new A();
  108. o.p;
  109. delete A.prototype.p;
  110. o.p;
  111. WScript.Echo(o.p);
  112. WScript.Echo(A.prototype.p);
  113. })();
  114. (function () {
  115. WScript.Echo("*** Setters, prototypes, and deleting properties ***");
  116. function A() { };
  117. A.prototype = {
  118. get p () { return this._p; },
  119. set p (v) { this._p = v; }
  120. };
  121. var o = new A();
  122. o._p = undefined; // create the property to stop the setter from changing the type
  123. o.p = 1;
  124. delete A.prototype.p;
  125. o.p = 2;
  126. WScript.Echo(o.p);
  127. WScript.Echo(A.prototype.p);
  128. })();