Google Apps Script(V8)是否支持私有类属性?
Google Apps Script(V8)私有类字段支持问题
问题背景
我尝试在Google Apps Script中使用私有类特性,编写了以下代码片段:
class Feed { #user; #password; url; constructor(url, credentials) { // class constructor this.url = url; this.#user = credentials.user; this.#password = credentials.password; } }
计划通过以下方式调用该类:
const url = "https://URL" const credentials = {user: 'user', password: 'password'}; let feed = new Feed(url, credentials);
保存文件时出现如下错误,似乎表明Google Apps Script不支持私有类特性:
Syntax error: ParseError: Unexpected token ILLEGAL line: 3 file: Testing.gs
我移除了第三行的#,修改后的代码如下:
class Feed { user; #password; url; constructor(url, credentials) { // class constructor this.url = url; this.#user = credentials.user; this.#password = credentials.password; } }
保存时又收到如下错误:
Syntax error: SyntaxError: Private field '#user' must be declared in an enclosing class line: 11 file: Testing.gs
我搜索发现早期资料显示Google Apps Script不支持私有类特性,第一个错误看似证实了这一点,但第二个错误却表明Apps Script能识别私有类字段。
核心问题
- Google Apps Script(V8)是否支持私有类字段?
- 如果支持,应如何正确实现?
内容的提问来源于stack exchange,提问作者Fergal Moore
相关产品推荐
相关产品推荐

