如何使用 javascript 在 Google 应用程序脚本中声明 Class 字段
How to declare Class fields in Google App Script using javascript
我正在尝试在 Google 脚本上创建一个包含某些字段的 class
。我什至无法保存文件。根据我对 的理解,我对 class 字段使用了正确的语法。
V8 runtime 已启用。
错误:
Syntax error: ParseError: Unexpected token = line: 5 file: Airtable_Class.gs
第 5 行是:foo = "bar";
完整代码如下:
class FieldsTest{
foo = "bar";
}
我试了一下,能够定义 class 设置如下:
class Test{
constructor(foo){
foo = "bar";
}
}
我正在查看 documentation and it suggest to build up classes like the ones shown in the reference。
这是一个已知问题。给问题加个星号(左上★),如果你想实现的话。
https://issuetracker.google.com/issues/195752915
根据tracker,是支持的,但是被解析器屏蔽了
尽管 Google Apps 脚本 不 支持 static class 字段,它支持静态methods/getters/setters,所以你可以这样做:
class X {
// ⭐️ static getters & setters
static get a(){ return this._a || 0 } // this === X
static set a(value){ this._a = value } // this === X
}
然后你可以 get/set X.a
照常:
X.a // get: 0.0
X.a = 3 // set: 3.0
X.a // get: 3.0
我正在尝试在 Google 脚本上创建一个包含某些字段的 class
。我什至无法保存文件。根据我对
V8 runtime 已启用。
错误:
Syntax error: ParseError: Unexpected token = line: 5 file: Airtable_Class.gs
第 5 行是:foo = "bar";
完整代码如下:
class FieldsTest{
foo = "bar";
}
我试了一下,能够定义 class 设置如下:
class Test{
constructor(foo){
foo = "bar";
}
}
我正在查看 documentation and it suggest to build up classes like the ones shown in the reference。
这是一个已知问题。给问题加个星号(左上★),如果你想实现的话。
https://issuetracker.google.com/issues/195752915
根据tracker,是支持的,但是被解析器屏蔽了
尽管 Google Apps 脚本 不 支持 static class 字段,它支持静态methods/getters/setters,所以你可以这样做:
class X {
// ⭐️ static getters & setters
static get a(){ return this._a || 0 } // this === X
static set a(value){ this._a = value } // this === X
}
然后你可以 get/set X.a
照常:
X.a // get: 0.0
X.a = 3 // set: 3.0
X.a // get: 3.0