SharedStorageWorkletGlobalScope: register() 方法
SharedStorageWorkletGlobalScope 介面的 register() 方法用於註冊在當前 worklet 模組中定義的 operation。
語法
js
register(name, operationCtor)
引數
name-
一個字串,表示您希望註冊該 operation 的名稱。當呼叫該 operation 時(例如透過
WindowSharedStorage.run()或WindowSharedStorage.selectURL()),此名稱將用於識別您想要執行的 operation。 operationCtor-
一個字串,表示要註冊的 operation 的類名。這是在 operation 執行時呼叫的類建構函式。
返回值
無 (undefined)。
異常
TypeError-
在以下情況下丟擲
- 已使用指定的名稱註冊了一個 operation。
operationCtor不是一個有效的建構函式。- 該類不包含有效的
run()方法。 - 尚未透過
SharedStorageWorklet.addModule()新增 worklet 模組。
示例
js
// ab-testing-worklet.js
class SelectURLOperation {
async run(urls, data) {
// Read the user's experiment group from shared storage
const experimentGroup = await this.sharedStorage.get("ab-testing-group");
// Return the group number
return experimentGroup;
}
}
register("ab-testing", SelectURLOperation);
有關此示例的演練以及指向其他示例的連結,請參閱 Shared Storage API 入門頁面。
注意: 可以在同一個共享儲存工作執行緒模組指令碼中定義和註冊多個具有不同名稱的操作;有關示例,請參閱SharedStorageOperation。
規範
| 規範 |
|---|
| 共享儲存 API # dom-sharedstorageworkletglobalscope-register |
瀏覽器相容性
載入中…