function Fun() {}
Fun.prototype = {
_players: null,
_rules: null,
_done: null,
iURL: 'https://example.com/',
iURL_on: 'https://example.com/',
get players() { // ← Getter (屬性訪問器)
if (!this._players) {
this._players = { // 惰性初始化
'biubiu_loader': this.iURL + 'loader.swf',
'biubiu_player': this.iURL + 'player.swf'
};
}
return this._players; // 返回對象引用
},
get rules() { // 另一個 Getter
if (!this._rules) {
this._rules = { ... };
}
return this._rules;
}
};
get / set 訪問器屬性| 特性 | 說明 |
|---|---|
| 語法 | get propName() { ... } / set propName(val) { ... } |
| 調用方式 | obj.propName (像訪問屬性,非函數調用 obj.propName()) |
| 執行時機 | 每次讀取屬性時執行 getter 函數體 |
| 返回值 | getter 的 return 值即為屬性值 |
this 指向 |
調用對象 (此處為 Fun 實例) |
| 可配置性 | 默認 configurable: true, enumerable: true |
obj.players = ... (除非定義 set),防止誤覆蓋內部狀態。this.iURL 拼接)。_players 緩存構建結果,後續訪問直接返回。// C#
public Dictionary<string, string> Players {
get {
if (_players == null) _players = BuildPlayers();
return _players;
}
}
// Java (無語法糖,慣用 getXxx())
public Map<String, String> getPlayers() {
if (players == null) players = buildPlayers();
return players;
}
代碼來源於 YoukuAntiADs / CleanPlayer 類去廣告腳本。
get players()返回播放器 SWF 地址映射,get rules()返回過濾規則。
腳本注入頁面後,攔截視頻播放器請求,替換為無廣告播放器地址。
Object.freeze(clone) 或使用 Proxy 代理。class PlayerConfig {
#players = null;
get players() {
if (!this.#players) this.#players = this.buildPlayers();
return this.#players;
}
}