FixedFieldsOnSingletons.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 globalFixedFunction1() {
  6. WScript.Echo("globalFixedFunction1: original");
  7. }
  8. var globalFixedFunction2 = function () {
  9. WScript.Echo("globalFixedFunction2: original");
  10. }
  11. function testGlobal() {
  12. globalFixedFunction1();
  13. globalFixedFunction2();
  14. }
  15. WScript.Echo("Testing the global object:");
  16. testGlobal();
  17. testGlobal();
  18. globalFixedFunction1 = function () {
  19. WScript.Echo("globalFixedFunction1: overwritten");
  20. }
  21. globalFixedFunction2 = function () {
  22. WScript.Echo("globalFixedFunction2: overwritten");
  23. }
  24. testGlobal();
  25. WScript.Echo();
  26. WScript.Echo("Testing object literal:");
  27. var objectLiteral = {
  28. unique1: 0,
  29. x: 0,
  30. y: 1,
  31. add: function () {
  32. return (this.x + this.y) + " (original)";
  33. },
  34. subtract: function () {
  35. return (this.x - this.y) + " (original)";
  36. }
  37. }
  38. function testObjectLiteral() {
  39. WScript.Echo("x + y = " + objectLiteral.add());
  40. WScript.Echo("x - y = " + objectLiteral.subtract());
  41. }
  42. testObjectLiteral();
  43. testObjectLiteral();
  44. objectLiteral.add = function () {
  45. return (this.x + this.y) + " (overwritten)";
  46. }
  47. testObjectLiteral();
  48. WScript.Echo();
  49. WScript.Echo("Testing Object.defineProperty with accessors:");
  50. var object = {};
  51. Object.defineProperty(object, "x", { get: function() { return "0 (original)"; }, configurable: true });
  52. function testObjectDefineProperty() {
  53. WScript.Echo("x = " + object.x);
  54. }
  55. testObjectDefineProperty();
  56. testObjectDefineProperty();
  57. Object.defineProperty(object, "x", { get: function () { return "1 (overwritten)"; } });
  58. testObjectDefineProperty();
  59. WScript.Echo();
  60. WScript.Echo("Testing the Math object:");
  61. Math.identity = function (value) {
  62. return value;
  63. }
  64. function testMathObject() {
  65. WScript.Echo("Math.sin(Math.PI) = " + Math.sin(Math.PI));
  66. WScript.Echo("Math.identity(Math.PI) = " + Math.identity(Math.PI));
  67. }
  68. testMathObject();
  69. testMathObject();
  70. Math.identity = function (value) {
  71. return -value;
  72. }
  73. testMathObject();
  74. Math.sin = function (value) {
  75. return -value;
  76. }
  77. testMathObject();
  78. WScript.Echo();
  79. WScript.Echo("Testing the Object constructor:");
  80. Object.identity = function (value) {
  81. return value;
  82. }
  83. function testObjectConstructor() {
  84. var o = {};
  85. Object.seal(o);
  86. WScript.Echo("Object.identity(o) = " + Object.identity(o));
  87. WScript.Echo("Object.isSealed(o) = " + Object.isSealed(o));
  88. }
  89. testObjectConstructor();
  90. testObjectConstructor();
  91. Object.identity = function (value) {
  92. return "I don't know you anymore...";
  93. }
  94. testObjectConstructor();
  95. Object.seal = function (object) {
  96. return false;
  97. }
  98. testObjectConstructor();
  99. Object.isSealed = function (object) {
  100. return "With the magic of JavaScript I pronounce you sealed!";
  101. }
  102. testObjectConstructor();
  103. WScript.Echo();