🔸class field
JS ⟩ class ⟩ definition ⟩ class fields
class fields are defined on the instance, not on the prototype.
👉 MDN ⟩ arrow functions used as methods
The class fields are initialized:
base class: before
constructor()derived class: immediately after
super()
👉 更詳細內容,請看: override class fields
override class fields is kind of tricky, watch out❗️
getter/setter behaves like a normal property.
static member may not be supported in all browsers.
can be used as a bound method via arrow function.
Google Apps Script 目前不支援 class field,但可用 (static/instance) getter/setter 代替。
About Google Apps Script Editor:
Google ⟩ Issue Tracker ⟩ static class property not recognised (won't fix)
問:「 Google Apps Script 目前 (2022.05) 不支援 static members,但是支援 class fields 嗎 」❓
答:「 Google Apps Script 不支援的是 class field(不管是 static 還是 instance),但神奇的是,它竟然支援 (static/instance) methods/getters/setters❗️」
💊 解藥:
因此,我們可以用以下方案解決 GAS 不支援 class field 的問題。
instance class fields:用 constructor 或用一般的 getters/setters。
static class fields:用 static 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
}
X.a // get: 0.0
X.a = 3 // set: 3.0
X.a // get: 3.0Last updated
Was this helpful?