0

In google app script,static is not a keyword,how to use this method in google app script?

class Test{
  constructor(a){
    this.a = a;
  }
  static trythis(b,c){
    return b*c;
  }
}


function test123(){
  let test = new Test(123)
  Logger.log(test)
  let test2 = trythis(2,3)
  Logger.log(test2)
} 

ReferenceError: trythis is not defined

https://i.stack.imgur.com/HovHd.png

Dan Chen
  • 3
  • 3
  • https://i.stack.imgur.com/HovHd.png – Dan Chen Sep 16 '21 at 04:21
  • 1
    Although Google Apps Script doesn't support **class fields**, it does support **static methods/getters/setters**, you can take this advantage and make a static getter/setter for each static property, please see: [this post](https://stackoverflow.com/a/72290771/5409815) for more information. – lochiwei May 18 '22 at 14:36

1 Answers1

3

Just like in JavaScript:

class Test{
  constructor(a){
    this.a = a;
  }
  static trythis(b,c){
    return b*c;
  }
}


function test123(){
  let test = new Test(123)
  console.log(test)
  let test2 = Test.trythis(2,3) // like this
  console.log(test2)
} 

test123();

EDIT

Proof that it works in Apps Script

enter image description here

Dmitry Kostyuk
  • 1,354
  • 1
  • 5
  • 21
  • In google app script, static is not a keyword. – Dan Chen Sep 16 '21 at 08:29
  • https://i.stack.imgur.com/HovHd.png – Dan Chen Sep 16 '21 at 08:29
  • You need to change '`trythis(2, 3)` to `Test.trythis(2, 3)`. Check out my edit in the answer, I have a screenshot to prove that it works. If you use `static`, you have to call the class without using the `new` keyword – Dmitry Kostyuk Sep 16 '21 at 08:40
  • 2
    Related: There are some limitations to GAS's support for the `static` keyword. See [ParseError: Unexpected token = when trying to save in google Apps Script editor](https://stackoverflow.com/questions/68446467/parseerror-unexpected-token-when-trying-to-save-in-google-apps-script-editor) – andrewJames Sep 16 '21 at 12:27