ImplicitConversions.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 toStrings;
  6. var valueOfs;
  7. var toStringCalled;
  8. var valueOfCalled;
  9. toStrings =
  10. [
  11. {},
  12. function ()
  13. {
  14. toStringCalled = true;
  15. return {};
  16. },
  17. function ()
  18. {
  19. toStringCalled = true;
  20. return undefined;
  21. },
  22. function ()
  23. {
  24. toStringCalled = true;
  25. return "hi";
  26. }
  27. ];
  28. valueOfs =
  29. [
  30. {},
  31. function ()
  32. {
  33. valueOfCalled = true;
  34. return {};
  35. },
  36. function ()
  37. {
  38. valueOfCalled = true;
  39. return undefined;
  40. },
  41. function ()
  42. {
  43. valueOfCalled = true;
  44. return "hi";
  45. },
  46. function ()
  47. {
  48. valueOfCalled = true;
  49. return "1/1/1970 1:00 am";
  50. },
  51. function ()
  52. {
  53. valueOfCalled = true;
  54. return "84";
  55. },
  56. function ()
  57. {
  58. valueOfCalled = true;
  59. return 37;
  60. }
  61. ];
  62. for (var ts in toStrings)
  63. {
  64. for (var vo in valueOfs)
  65. {
  66. toStringCalled = false;
  67. valueOfCalled = false;
  68. var obj = { toString: toStrings[ts], valueOf: valueOfs[vo] };
  69. WScript.Echo("=== Implicit toString ===");
  70. try
  71. {
  72. WScript.Echo("" + obj);
  73. }
  74. catch (ex)
  75. {
  76. WScript.Echo("Got error:");
  77. WScript.Echo(" name: " + ex.name);
  78. WScript.Echo(" message: " + ex.message);
  79. }
  80. WScript.Echo("toString called: " + (toStringCalled ? "yes" : "no"));
  81. WScript.Echo("valueOf called: " + (valueOfCalled ? "yes" : "no"));
  82. WScript.Echo("=== Implicit valueOf ===");
  83. try
  84. {
  85. WScript.Echo(1 * obj);
  86. }
  87. catch (ex)
  88. {
  89. WScript.Echo("Got error:");
  90. WScript.Echo(" name: " + ex.name);
  91. WScript.Echo(" message: " + ex.message);
  92. }
  93. WScript.Echo("toString called: " + (toStringCalled ? "yes" : "no"));
  94. WScript.Echo("valueOf called: " + (valueOfCalled ? "yes" : "no"));
  95. }
  96. }