• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Use of 'const' in ExtendScript

Explorer ,
Jun 19, 2013 Jun 19, 2013

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 19, 2013 Jun 19, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 20, 2013 Jun 20, 2013

Copy link to clipboard

Copied

Adobe ExtendScript Toolkit CS6

$.version: 4.2.12

$.build: 66.489054

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 26, 2013 Jun 26, 2013

Copy link to clipboard

Copied

LATEST

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines