0

I want to create a class that has private methods. For example:

class RabbitDefiner {
  #define(){
    return "a rodent";
  }
}

I get:

Syntax error: ParseError: Unexpected token ILLEGAL

In other threads I've seen that private fields aren't implemented in GAS (why??) and there are some workarounds. I'm not sure how to apply those workarounds to methods. Has anyone found a solution to this?

Robert M.
  • 575
  • 5
  • 17
  • 2
    Hint: a private method is just a private field that happens to be a function. – Heretic Monkey Jun 24 '23 at 01:26
  • 1
    [This similar question](https://stackoverflow.com/q/73055980) doesn't have an answer yet. – InSync Jun 24 '23 at 02:21
  • 1
    Private class fields and methods are not yet supported in GAS. GAS seems to run on an older version of V8 so its not always current when it comes to feature parity. They'll probably update it some time in the future. Best you can do right now is to leverage closures and/or WeakMaps. – TheAddonDepot Jun 24 '23 at 14:28
  • @HereticMonkey I suppose that's true. I come from C++ where things aren't so loosey-goosey. – Robert M. Jun 24 '23 at 19:31

0 Answers0