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:
Do you need the "Constructor" keyword?