2

I attempted to use Private class features in Google Apps Script, using the following snippet:

class Feed
{
  #user;
  #password;
  url;
  
  constructor(url, credentials)
  {
    // class constructor
    this.url = url;
    this.#user = credentials.user;
    this.#password = credentials.password;
  }
}

with the intention of calling it in the following manner:

const url = "https://URL"
const credentials = {user: 'user', password: 'password'};

let feed = new Feed(url, credentials);

On saving the file, the following error was displayed, appearing to indicate that private class features aren't supported in Google Apps Script:

Syntax error: ParseError: Unexpected token ILLEGAL line: 3 file: Testing.gs

I removed the '#' on line three, as shown below:

class Feed
{
  user;
  #password;
  url;
  
  constructor(url, credentials)
  {
    // class constructor
    this.url = url;
    this.#user = credentials.user;
    this.#password = credentials.password;
  }
}

and received the following error on saving:

Syntax error: SyntaxError: Private field '#user' must be declared in an enclosing class line: 11 file: Testing.gs

I searched, and found Define private class fields with Google Apps Scripts (GAS) V8 which indicated that (at the time) private class features weren't supported in Google Apps Script. While the first error message seems to confirm that this is the case, the second implies that Apps Script understands private class fields.

Questions:

  • Are private class fields supported by Google Apps Script (v8)?
  • If so, how are they supposed to be implemented?

Thanks,

Fergal

3
  • 1
    Private class fields are probably not supported yet (even though the error suggests otherwise). You can fallback on using closures and WeakMaps for the time being. Jul 20, 2022 at 18:22
  • 1
    I reported the inconsistent error messaging via issuetracker.google.com/issues/239767510. We'll see what they come back with. Jul 21, 2022 at 10:57
  • 1
    Confirmed as a known issue, and something that they are working on, but not available yet. Jul 21, 2022 at 14:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy