extensible.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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(args)
  6. {
  7. WScript.Echo(args);
  8. }
  9. write("TestCase1");
  10. write(Object.preventExtensions.length);
  11. write(Object.isExtensible({}));
  12. write("TestCase2 - preventExtensions & add a property");
  13. var a = {x:20, y:30};
  14. Object.preventExtensions(a);
  15. SafeCall(function() { a.z = 50; });
  16. write(Object.getOwnPropertyNames(a));
  17. write(Object.isExtensible(a));
  18. write("TestCase3 - preventExtensions & delete a property");
  19. var a = {x:20, y:30};
  20. Object.preventExtensions(a);
  21. SafeCall(function() { delete a.x; });
  22. write(a.x);
  23. write(Object.isExtensible(a));
  24. write("TestCase4 - preventExtensions & modify a property");
  25. var a = {x:20, y:30};
  26. Object.preventExtensions(a);
  27. SafeCall(function() { a.x = 40; });
  28. SafeCall(function() { a.y = 60; });
  29. write(Object.getOwnPropertyNames(a));
  30. write(Object.isExtensible(a));
  31. write(a.x);
  32. write("TestCase5 - preventExtension on global object & declare a var");
  33. Object.preventExtensions(this);
  34. var newVar1 = 4; // No exception here, since var decl is hoisted
  35. try
  36. {
  37. eval("var newVar2"); // Should throw TypeError
  38. }
  39. catch (e)
  40. {
  41. write("Exception: " + e.name);
  42. }
  43. write("TestCase6 - preventExtensions, delete property and set remaining properties to non configurable/writable - SimpleDictionaryTypeHandler");
  44. var a = {x:20, y:30};
  45. Object.preventExtensions(a);
  46. delete a.x;
  47. Object.defineProperty(a, "y", {configurable: false});
  48. write(Object.isSealed(a));
  49. Object.defineProperty(a, "y", {writable: false});
  50. write(Object.isFrozen(a));
  51. write("TestCase7 - preventExtensions, delete property and set remaining properties to non configurable/writable - DictionaryTypeHandler");
  52. var a = {get x() {return 0;}, y:30};
  53. Object.preventExtensions(a);
  54. delete a.x;
  55. Object.defineProperty(a, "y", {configurable: false});
  56. write(Object.isSealed(a));
  57. Object.defineProperty(a, "y", {writable: false});
  58. write(Object.isFrozen(a));
  59. function SafeCall(f)
  60. {
  61. try
  62. {
  63. f();
  64. }
  65. catch (e)
  66. {
  67. write("Exception: " + e.name);
  68. }
  69. }
  70. Object.preventExtensions(this);
  71. this[10]=10; //GlobalObject set after preventExtensions