Is there a reason to use CONST?
  • SaveremreveSaveremreve January 2013

    I'm working alongside a gameplay programmer that did some moonlighting on export scripts.  Their code makes extensive use of CONST for various URI and filename elements.  The problem I'm encountering is that every 4 - 5 save/load loops to test Flash will throw an error about redeclaring a CONST and I have to restart flash to get the script to run again.  Error Text:

    TypeError: redeclaration of const IFL_FILE_EXT 

    This same script also will throw errors running through the snippets panel (with flash gobbling the error message) but when I run it through commands>run it usually will run without errors.  Right now it's just a hunch that CONST is causing trouble - since I don't use it and it runs fine.  Searches elsewhere on the web say that CONST implementation in Javascript is spotty so if there's reasons not to do it I'd just as well pull it out.

    edit: added error text

  • DaveDave January 2013

    Quick answer: no, avoid it.

    Long answer: Generally, using a CONST will cause all subsequent redeclarations to fail. This is the same as After Effects. Seems like it will do you a favour, in reality, it doesn't.

    The problem here is to down to scope (or sessions, or state, whatever you want to call it). Externally-run code will run in a new scope / session, however, SWF panels, like Snippets, have a single persistent scope so redcalaring a const will, as you say, throw an error.

    The reason for this is that the Snippets panel pretty much just loads in whatever script it references, and runs it, in the scope of the panel, so running the same script twice will attempt to redeclare the value.

    To check this, create a new Snippet and only put in...


    ... and you'll see all the variables that have been created since the panel was opened. Contrast that with running scripts externally, and you'll see that variables are not persistent. Strange though, as I thought Commands were persistent. Perhaps this changed in CS6 - I'll have to check!

    I must say, I don't use consts and just stick to the "var UPPER_CASE = " convention - and have never had an issue with either intention or execution :)

  • DaveDave January 2013

    Also, a couple of points.

    1. No need to restart Flash, just restart the Snippets panel (I map mine to a shortcut key). You may need to restart with Commands though.
    2. Rather than creating consts, I generally prefer to create an Object with static properties. That way, everything is namespaced, and you get a tider autocomplete in Komodo Edit as well.


  • SaveremreveSaveremreve January 2013

    Awesome!  Thanks so much.  My research seemed to point in this direction but I didn't want to go gut the code on a hunch. 

  • DaveDave January 2013

    FYI, I took another look at variable persistence in Commands.

    Code executed via Commands follow the following behaviour:

    • If a document is open, variables are persistent per document. This means you can only declare a constant once, per open document
    • If no documents are open, variables have no persistence (which is why I was unsure in the above post)

    SWF panels (including Snippets) still declare all variables in a single scope, and they are persistent.

    So, thanks for raising this, as it's allowed me to be a bit clearer with my explanation in the docs, and has flagged the difference between not having documents open, and having documents open for Commands.

    Now, go forth and gut!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with OpenID Sign In with Google Sign In with Twitter

Sign In Apply for Membership