3 Replies Latest reply on Jun 26, 2013 8:42 AM by dln385

    Use of 'const' in ExtendScript

    dln385 Level 2

      Let's start with an example.

       

      const a = 1;
      a = 2;
      $.writeln(a); // Prints 1.
      

       

      Here we can see that 'const' does indeed work. If you want to run this more than once in the ExtendScript Toolkit, be sure to select Debug > Reset from the menu.

       

      Another example:

       

      $.strict = true;
      const a = 1;
      a = 2; // ReferenceError: a is read only
      

       

      If strict mode is disabled, reassigning a constant value fails silently. If strict mode is enabled, reassigning a constant value throws an error.

       

      Here's where it starts to get weird:

       

      (function() {
          const a = 1;
          a = 2;
          $.writeln(a); // Prints 2.
      })();
      

       

      Huh? Suddenly 'const' has no effect! Even strict mode doesn't cause the reassignment to fail.

       

      In the earlier examples, "a" is actually a member of $.global. So I got this bright idea:

       

      var object = {};
      object.a = null;
      with (object) {
          const a = 1;
      }
      object.a = 2;
      $.writeln(object.a); // Prints 1.
      

       

      "a" is constant again! The "with" block locks the value of "a" so it can't be reassigned.

       

      So here's the general rule: Only object properties can be made constant. Local variables cannot be made constant. This means that closure variables cannot be made constant.

       

      To double-check this, I put the previous code inside a function:

       

      (function() {
          var object = {};
          object.a = null;
          with (object) {
              const a = 1;
          }
          object.a = 2;
          $.writeln(object.a); // Prints 1.
      })();
      

       

      No problem!

       

      Note that this behavior is not at all like JavaScript. In fact, it's the exact opposite of JavaScript. But still, it's very useful!

       

      Since "with" is considered harmful, it would be nice if we could create constants without using "with". However, due to the syntax requirements of "const", I don't think there's any getting around it.

        • 1. Re: Use of 'const' in ExtendScript
          Vamitul Level 4

          hello!

           

          a quick couple of questions:

           

          In what have you tested this?

          As far as i know, in Indesign's implementation of ExtendScript (CS5 at least), strict mode is ignored.

          • 2. Re: Use of 'const' in ExtendScript
            dln385 Level 2

            Adobe ExtendScript Toolkit CS6

            $.version: 4.2.12

            $.build: 66.489054

            $.buildDate: Mon Dec 05 2011 15:11:07 GMT-0500

            • 3. Re: Use of 'const' in ExtendScript
              dln385 Level 2

              I discovered an unexpected and undesired side-effect of using constants in ExtendScript:

               

              var object = {};
              object.a = null;
              with (object) {
                  const a = 1;
              }
              object.a = 2; // Throws "ReferenceError: a is read only" if strict mode is enabled.
              $.writeln(object.a); // Prints 1.
              // As a side effect, the local variable 'a' now exists and holds the value 'undefined'.
              

               

              Even though the 'const' keyword was being applied to 'object.a', as a side effect the local variable 'a' was declared. To my knowledge it's impossible to delete local variables, so there's no getting around it. This means that making a global variable constant is effectively useless (though that seems like it would be a bad idea anyway).