forInPrimitive.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. //
  6. // Test for-in enumerating properties on prototype of primitives
  7. //
  8. var echo = WScript.Echo;
  9. function guard(f) {
  10. try {
  11. f();
  12. } catch (e) {
  13. echo(e);
  14. }
  15. }
  16. Object.prototype.object_value = "value on Object.prototype";
  17. Object.defineProperty(
  18. Object.prototype, "object_getter", {
  19. get: function () { return "getter on Object.prototype: " + typeof(this) + " " + this; },
  20. enumerable: true,
  21. configurable: true
  22. });
  23. Number.prototype.number_proto = "Value on Number.prototype";
  24. Boolean.prototype.boolean_proto = "Value on Boolean.prototype";
  25. String.prototype.string_proto = "Value on String.prototype";
  26. // Test on special values and primitives
  27. var tests = [
  28. null,
  29. undefined,
  30. Number.NaN,
  31. 0.4,
  32. -0,
  33. 0,
  34. 1,
  35. "",
  36. true,
  37. false,
  38. // Special values around Int32/Int31 boundary
  39. 0x80000000,
  40. 0x7FFFFFFF,
  41. 0x40000000,
  42. 0x3FFFFFFF,
  43. -0x3FFFFFFF,
  44. -0x40000000,
  45. -0x40000001,
  46. -0x80000000,
  47. -0x80000001,
  48. ];
  49. tests.forEach(function (a) {
  50. echo("---- Test:", a, "----");
  51. guard(function () {
  52. echo(a.object_value); // Get from prototype
  53. });
  54. guard(function () {
  55. echo(a.object_getter); // Getter on prototype
  56. });
  57. echo();
  58. for (var p in a) { // Enumerate properties should walk prototype
  59. echo(" ", p);
  60. }
  61. echo();
  62. });