ParseError: Unexpected token = when trying to save in google Apps Script editor

616 views
Skip to first unread message

Jack Z

unread,
Jul 19, 2021, 5:48:25 PM7/19/21
to Google Apps Script Community
I reduced the issue to this simple bit of code:

class X { static a = 5; }

I don't know why this is a problem. It seems to be valid code. Any ideas?

VXMzI[1].png
Message has been deleted
Message has been deleted

dimud...@gmail.com

unread,
Jul 20, 2021, 8:23:15 AM7/20/21
to Google Apps Script Community
That's not valid code as GAS does not (yet?) support static member variables in class definitions.
However you can leverage a static getter function as follows:

/**
 * An enumerated type mapping color names to
 * its corresponding HTML code
 */
class Colors {
    static get RED() { return '#FF0000'; }
    static get GREEN() { return '#00FF00'; }
    static get BLUE() { return '#0000FF'; }

console.log(Colors.RED); // logs the string '#FF0000'


The above works well for immutable properties (great for enumerations). However if you want static properties that are mutable you'll have to do some finagling to get it to work using getter/setters. You can leverage an IIFE(Immediately Invoked Function Expression) to achieve that:

const Mutable = (function(){
    let state = { value: 5};
    return class Mutable {
        static get state() { return state.value; };
        static set state(v) { state.value = v };
    };
})();


However, more often than not, a Plain-Old-Javascript-Object(POJO) gets the job done;

let obj = { a: 5 };
On Monday, July 19, 2021 at 5:48:25 PM UTC-4 Jack Z wrote:
On Tuesday, July 20, 2021 at 7:54:36 AM UTC-4 cwl...@gmail.com wrote:
Do you need the "Constructor" keyword?

Jack Z

unread,
Jul 20, 2021, 8:24:33 AM7/20/21
to Google Apps Script Community
  > That's not valid code as GAS does not (yet?) support static member variables in class definitions.

Is this not valid code, or does GAS just not recognize this valid code?  If the former, then what is the correct code.  If the latter, then this is a bug and should be reported.

> However you can leverage a static getter function as follows:

Yeah. Thought of that, but that would require generating that object every time I want that item, which isn't what I wanted.

> However, more often than not, a Plain-Old-Javascript-Object(POJO) gets the job done;

I'm not sure what you are trying to say here.  That would make `a` the equivalent of a member variable, which isn't what I wanted.  I want a class with a static member variable.  I only stripped out everything else to make a MWE 

I know I can do this workaround:

class X { }
X.a = 5;

But I wanted to know if this was a problem with GAS or with what I was writing.

Thx.

BTW, how did you colorize your code?  Did you paste from somewhere else which autocolorizes it (and from where) or you do it manually?

dimud...@gmail.com

unread,
Jul 20, 2021, 8:26:36 AM7/20/21
to Google Apps Script Community
Correcting some typos in my previous post:

const Mutable = (function(){
    let state = { value: 5};
    return class Mutable {
        static get state() { return state.value; }
        static set state(v) { state.value = v; }
    };
})();
On Tuesday, July 20, 2021 at 8:23:15 AM UTC-4 dimud...@gmail.com wrote:

Jack Adrian Zappa

unread,
Jul 20, 2021, 8:55:32 AM7/20/21
to google-apps-sc...@googlegroups.com
const Mutable = (function(){
    let state = { value: 5};
    return class Mutable {
        static get state() { return state.value; }
        static set state(v) { state.value = v; }
    };
})();

Oh, I see what you are doing here.  Yeah, this doesn't regenerate the object at each getter call, but really complicates the code unnecessarily.  Thx.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/e5564906-16ab-4ed4-87cf-2fd91ae5ff46n%40googlegroups.com.

Jack Z

unread,
Jul 20, 2021, 9:01:32 AM7/20/21
to Google Apps Script Community
As this seems to be a bug, I've reported it here:  https://issuetracker.google.com/u/1/issues/194120497

dimud...@gmail.com

unread,
Jul 20, 2021, 11:53:12 AM7/20/21
to Google Apps Script Community
Keep in mind that static public fields are relatively new to Javascript so it may take a while before we get that stuff in GAS (hopefully we won't have to wait years for an update...)

Jack Z

unread,
Jul 20, 2021, 7:26:58 PM7/20/21
to Google Apps Script Community
I would have thought that static public fields would include member functions and variables.  I was under the impression that they were treated the same way more or less.  Also, the editor actually even autocompletes correctly with a static member variable too.  So it would seem that they are doing related things with different code bases.

Anyway, it is a bug and it has been posted for them to keep track of, so that they can correct it later.
Reply all
Reply to author
Forward
0 new messages