constreassign1.js 1.5 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. const a = 1;
  6. with({a:2}) {
  7. a++;
  8. print(a); // 3
  9. }
  10. try {
  11. with({b:2}) {
  12. a++;
  13. print(a);
  14. }
  15. }
  16. catch(e) {
  17. print(e); // TypeError: Assignment to const
  18. }
  19. let foo1 = new Function(
  20. "with({a:2}) {" +
  21. " a++;" +
  22. " print(a);" +
  23. "}");
  24. foo1(); // 3
  25. let foo2 = new Function(
  26. "with({b:2}) {" +
  27. " a++;" +
  28. " print(a);" +
  29. "}");
  30. try {
  31. foo2();
  32. }
  33. catch(e) {
  34. print(e); // TypeError: Assignment to const
  35. }
  36. try {
  37. eval('let b = 3');
  38. a++;
  39. print(a);
  40. }
  41. catch(e) {
  42. print(e); // TypeError: Assignment to const
  43. }
  44. (function() {
  45. const a = 1;
  46. with({a:2}) {
  47. a++;
  48. print(a); // 3
  49. }
  50. try {
  51. with({b:2}) {
  52. a++;
  53. print(a);
  54. }
  55. }
  56. catch(e) {
  57. print(e); // TypeError: Assignment to const
  58. }
  59. try {
  60. eval('let b = 3');
  61. a++;
  62. print(a);
  63. }
  64. catch(e) {
  65. print(e); // TypeError: Assignment to const
  66. }
  67. })();
  68. function print(x) { WScript.Echo(x) }