1# 启动一个worker<a name="ZH-CN_TOPIC_0000001164966380"></a> 2 3> **说明:** 4>从 API Version 7 开始支持。 5 6## 导入模块<a name="zh-cn_topic_0000001210206047_s56d19203690d4782bfc74069abb6bd71"></a> 7 8``` 9import worker from '@ohos.worker'; 10``` 11 12## 权限<a name="zh-cn_topic_0000001210206047_section11257113618419"></a> 13 14无 15 16## 属性<a name="zh-cn_topic_0000001210206047_section1578718135716"></a> 17 18<a name="zh-cn_topic_0000001210206047_table17771723175716"></a> 19<table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row1977112237573"><th class="cellrowborder" valign="top" width="15.870000000000001%" id="mcps1.1.6.1.1"><p id="zh-cn_topic_0000001210206047_p12771142335710"><a name="zh-cn_topic_0000001210206047_p12771142335710"></a><a name="zh-cn_topic_0000001210206047_p12771142335710"></a>名称</p> 20</th> 21<th class="cellrowborder" valign="top" width="18.19%" id="mcps1.1.6.1.2"><p id="zh-cn_topic_0000001210206047_p12771202325716"><a name="zh-cn_topic_0000001210206047_p12771202325716"></a><a name="zh-cn_topic_0000001210206047_p12771202325716"></a>参数类型</p> 22</th> 23<th class="cellrowborder" valign="top" width="11.64%" id="mcps1.1.6.1.3"><p id="zh-cn_topic_0000001210206047_p1577117232576"><a name="zh-cn_topic_0000001210206047_p1577117232576"></a><a name="zh-cn_topic_0000001210206047_p1577117232576"></a>可读</p> 24</th> 25<th class="cellrowborder" valign="top" width="10.31%" id="mcps1.1.6.1.4"><p id="zh-cn_topic_0000001210206047_p2771623185711"><a name="zh-cn_topic_0000001210206047_p2771623185711"></a><a name="zh-cn_topic_0000001210206047_p2771623185711"></a>可写</p> 26</th> 27<th class="cellrowborder" valign="top" width="43.99%" id="mcps1.1.6.1.5"><p id="zh-cn_topic_0000001210206047_p197711223155714"><a name="zh-cn_topic_0000001210206047_p197711223155714"></a><a name="zh-cn_topic_0000001210206047_p197711223155714"></a>说明</p> 28</th> 29</tr> 30</thead> 31<tbody><tr id="zh-cn_topic_0000001210206047_row877142318577"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p1771192315710"><a name="zh-cn_topic_0000001210206047_p1771192315710"></a><a name="zh-cn_topic_0000001210206047_p1771192315710"></a>parentPort</p> 32</td> 33<td class="cellrowborder" valign="top" width="18.19%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p396011395812"><a name="zh-cn_topic_0000001210206047_p396011395812"></a><a name="zh-cn_topic_0000001210206047_p396011395812"></a><a href="#zh-cn_topic_0000001210206047_section36872023123912">DedicatedWorkerGlobalScope</a></p> 34</td> 35<td class="cellrowborder" valign="top" width="11.64%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p677122335711"><a name="zh-cn_topic_0000001210206047_p677122335711"></a><a name="zh-cn_topic_0000001210206047_p677122335711"></a>是</p> 36</td> 37<td class="cellrowborder" valign="top" width="10.31%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p17719232577"><a name="zh-cn_topic_0000001210206047_p17719232577"></a><a name="zh-cn_topic_0000001210206047_p17719232577"></a>是</p> 38</td> 39<td class="cellrowborder" valign="top" width="43.99%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p205212394586"><a name="zh-cn_topic_0000001210206047_p205212394586"></a><a name="zh-cn_topic_0000001210206047_p205212394586"></a>worker线程用于与宿主线程通信的对象。</p> 40</td> 41</tr> 42</tbody> 43</table> 44 45## WorkerOptions<a name="zh-cn_topic_0000001210206047_section189341937163212"></a> 46 47worker构造函数函数的选项信息,用于为worker添加其他信息。 48 49<a name="zh-cn_topic_0000001210206047_table728861911417"></a> 50<table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row62887198149"><th class="cellrowborder" valign="top" width="15.870000000000001%" id="mcps1.1.6.1.1"><p id="zh-cn_topic_0000001210206047_p928811911410"><a name="zh-cn_topic_0000001210206047_p928811911410"></a><a name="zh-cn_topic_0000001210206047_p928811911410"></a>名称</p> 51</th> 52<th class="cellrowborder" valign="top" width="18.19%" id="mcps1.1.6.1.2"><p id="zh-cn_topic_0000001210206047_p8288191916149"><a name="zh-cn_topic_0000001210206047_p8288191916149"></a><a name="zh-cn_topic_0000001210206047_p8288191916149"></a>参数类型</p> 53</th> 54<th class="cellrowborder" valign="top" width="11.64%" id="mcps1.1.6.1.3"><p id="zh-cn_topic_0000001210206047_p14288131911418"><a name="zh-cn_topic_0000001210206047_p14288131911418"></a><a name="zh-cn_topic_0000001210206047_p14288131911418"></a>可读</p> 55</th> 56<th class="cellrowborder" valign="top" width="10.31%" id="mcps1.1.6.1.4"><p id="zh-cn_topic_0000001210206047_p1928861921411"><a name="zh-cn_topic_0000001210206047_p1928861921411"></a><a name="zh-cn_topic_0000001210206047_p1928861921411"></a>可写</p> 57</th> 58<th class="cellrowborder" valign="top" width="43.99%" id="mcps1.1.6.1.5"><p id="zh-cn_topic_0000001210206047_p728815191149"><a name="zh-cn_topic_0000001210206047_p728815191149"></a><a name="zh-cn_topic_0000001210206047_p728815191149"></a>说明</p> 59</th> 60</tr> 61</thead> 62<tbody><tr id="zh-cn_topic_0000001210206047_row528891919142"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p3288191991414"><a name="zh-cn_topic_0000001210206047_p3288191991414"></a><a name="zh-cn_topic_0000001210206047_p3288191991414"></a>type</p> 63</td> 64<td class="cellrowborder" valign="top" width="18.19%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p3288121901418"><a name="zh-cn_topic_0000001210206047_p3288121901418"></a><a name="zh-cn_topic_0000001210206047_p3288121901418"></a>"classic"</p> 65</td> 66<td class="cellrowborder" valign="top" width="11.64%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p182881319191414"><a name="zh-cn_topic_0000001210206047_p182881319191414"></a><a name="zh-cn_topic_0000001210206047_p182881319191414"></a>是</p> 67</td> 68<td class="cellrowborder" valign="top" width="10.31%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p1176125118471"><a name="zh-cn_topic_0000001210206047_p1176125118471"></a><a name="zh-cn_topic_0000001210206047_p1176125118471"></a>是</p> 69</td> 70<td class="cellrowborder" valign="top" width="43.99%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p1428811192144"><a name="zh-cn_topic_0000001210206047_p1428811192144"></a><a name="zh-cn_topic_0000001210206047_p1428811192144"></a>按照指定方式执行脚本。</p> 71</td> 72</tr> 73<tr id="zh-cn_topic_0000001210206047_row11288719111417"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p10288419131415"><a name="zh-cn_topic_0000001210206047_p10288419131415"></a><a name="zh-cn_topic_0000001210206047_p10288419131415"></a>name</p> 74</td> 75<td class="cellrowborder" valign="top" width="18.19%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p428914193147"><a name="zh-cn_topic_0000001210206047_p428914193147"></a><a name="zh-cn_topic_0000001210206047_p428914193147"></a>string</p> 76</td> 77<td class="cellrowborder" valign="top" width="11.64%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p3289121912146"><a name="zh-cn_topic_0000001210206047_p3289121912146"></a><a name="zh-cn_topic_0000001210206047_p3289121912146"></a>是</p> 78</td> 79<td class="cellrowborder" valign="top" width="10.31%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p8289319111415"><a name="zh-cn_topic_0000001210206047_p8289319111415"></a><a name="zh-cn_topic_0000001210206047_p8289319111415"></a>是</p> 80</td> 81<td class="cellrowborder" valign="top" width="43.99%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p1228991915144"><a name="zh-cn_topic_0000001210206047_p1228991915144"></a><a name="zh-cn_topic_0000001210206047_p1228991915144"></a>worker的名称。</p> 82</td> 83</tr> 84</tbody> 85</table> 86 87## constructor<a name="zh-cn_topic_0000001210206047_section192192415554"></a> 88 89constructor\(scriptURL: string, options?: WorkerOptions\) 90 91worker构造函数。 92 93- 参数: 94 95 <a name="zh-cn_topic_0000001210206047_table69661135912"></a> 96 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row149668318915"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p7966738914"><a name="zh-cn_topic_0000001210206047_p7966738914"></a><a name="zh-cn_topic_0000001210206047_p7966738914"></a>参数名</p> 97 </th> 98 <th class="cellrowborder" valign="top" width="20.9%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p296713699"><a name="zh-cn_topic_0000001210206047_p296713699"></a><a name="zh-cn_topic_0000001210206047_p296713699"></a>类型</p> 99 </th> 100 <th class="cellrowborder" valign="top" width="10.77%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p196718315911"><a name="zh-cn_topic_0000001210206047_p196718315911"></a><a name="zh-cn_topic_0000001210206047_p196718315911"></a>必填</p> 101 </th> 102 <th class="cellrowborder" valign="top" width="53.510000000000005%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p9967231197"><a name="zh-cn_topic_0000001210206047_p9967231197"></a><a name="zh-cn_topic_0000001210206047_p9967231197"></a>说明</p> 103 </th> 104 </tr> 105 </thead> 106 <tbody><tr id="zh-cn_topic_0000001210206047_row99671533914"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p79671633910"><a name="zh-cn_topic_0000001210206047_p79671633910"></a><a name="zh-cn_topic_0000001210206047_p79671633910"></a>scriptURL</p> 107 </td> 108 <td class="cellrowborder" valign="top" width="20.9%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p11967433914"><a name="zh-cn_topic_0000001210206047_p11967433914"></a><a name="zh-cn_topic_0000001210206047_p11967433914"></a>string</p> 109 </td> 110 <td class="cellrowborder" valign="top" width="10.77%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p19671336916"><a name="zh-cn_topic_0000001210206047_p19671336916"></a><a name="zh-cn_topic_0000001210206047_p19671336916"></a>是</p> 111 </td> 112 <td class="cellrowborder" valign="top" width="53.510000000000005%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p69671631796"><a name="zh-cn_topic_0000001210206047_p69671631796"></a><a name="zh-cn_topic_0000001210206047_p69671631796"></a>worker执行脚本的url,路径规范:若DevEco新建工程在pages同级下没有workers目录,需要新建workers目录,将脚本文件放入workers目录。</p> 113 </td> 114 </tr> 115 <tr id="zh-cn_topic_0000001210206047_row18967831393"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p39671131590"><a name="zh-cn_topic_0000001210206047_p39671131590"></a><a name="zh-cn_topic_0000001210206047_p39671131590"></a>options</p> 116 </td> 117 <td class="cellrowborder" valign="top" width="20.9%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p126051952172518"><a name="zh-cn_topic_0000001210206047_p126051952172518"></a><a name="zh-cn_topic_0000001210206047_p126051952172518"></a><a href="#zh-cn_topic_0000001210206047_section189341937163212">WorkerOptions</a></p> 118 </td> 119 <td class="cellrowborder" valign="top" width="10.77%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p149671932919"><a name="zh-cn_topic_0000001210206047_p149671932919"></a><a name="zh-cn_topic_0000001210206047_p149671932919"></a>否</p> 120 </td> 121 <td class="cellrowborder" valign="top" width="53.510000000000005%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p19675312911"><a name="zh-cn_topic_0000001210206047_p19675312911"></a><a name="zh-cn_topic_0000001210206047_p19675312911"></a>worker构造的选项。</p> 122 </td> 123 </tr> 124 </tbody> 125 </table> 126 127- 返回值: 128 129 <a name="zh-cn_topic_0000001210206047_table16391145317913"></a> 130 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row2391145319910"><th class="cellrowborder" valign="top" width="20.990000000000002%" id="mcps1.1.3.1.1"><p id="zh-cn_topic_0000001210206047_p143913533916"><a name="zh-cn_topic_0000001210206047_p143913533916"></a><a name="zh-cn_topic_0000001210206047_p143913533916"></a>参数名</p> 131 </th> 132 <th class="cellrowborder" valign="top" width="79.01%" id="mcps1.1.3.1.2"><p id="zh-cn_topic_0000001210206047_p193911531395"><a name="zh-cn_topic_0000001210206047_p193911531395"></a><a name="zh-cn_topic_0000001210206047_p193911531395"></a>说明</p> 133 </th> 134 </tr> 135 </thead> 136 <tbody><tr id="zh-cn_topic_0000001210206047_row1339114531391"><td class="cellrowborder" valign="top" width="20.990000000000002%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0000001210206047_p173918533911"><a name="zh-cn_topic_0000001210206047_p173918533911"></a><a name="zh-cn_topic_0000001210206047_p173918533911"></a>worker</p> 137 </td> 138 <td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0000001210206047_p1039217531898"><a name="zh-cn_topic_0000001210206047_p1039217531898"></a><a name="zh-cn_topic_0000001210206047_p1039217531898"></a>执行Worker构造函数生成的Worker对象,失败则返回undefined。</p> 139 </td> 140 </tr> 141 </tbody> 142 </table> 143 144- 示例: 145 146 ``` 147 import worker from '@ohos.worker'; 148 const worker = new worker.Worker("workers/worker.js", {name:"first worker"}; 149 ``` 150 151 152## Worker<a name="zh-cn_topic_0000001210206047_section182179935414"></a> 153 154使用以下方法前,均需先构造worker实例,Worker类继承[EventTarget](#zh-cn_topic_0000001210206047_section3283104561014)。 155 156### postMessage<a name="zh-cn_topic_0000001210206047_section1410593619146"></a> 157 158postMessage\(message: Object, options?: PostMessageOptions\): void 159 160向worker线程发送消息,数据的传输采用结构化克隆算法。 161 162- 参数: 163 164 <a name="zh-cn_topic_0000001210206047_table114831131512"></a> 165 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row914851141517"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p214819113159"><a name="zh-cn_topic_0000001210206047_p214819113159"></a><a name="zh-cn_topic_0000001210206047_p214819113159"></a>参数名</p> 166 </th> 167 <th class="cellrowborder" valign="top" width="23.06%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p1214921121519"><a name="zh-cn_topic_0000001210206047_p1214921121519"></a><a name="zh-cn_topic_0000001210206047_p1214921121519"></a>类型</p> 168 </th> 169 <th class="cellrowborder" valign="top" width="10.870000000000001%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p11491711141512"><a name="zh-cn_topic_0000001210206047_p11491711141512"></a><a name="zh-cn_topic_0000001210206047_p11491711141512"></a>必填</p> 170 </th> 171 <th class="cellrowborder" valign="top" width="51.24999999999999%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p171491011171512"><a name="zh-cn_topic_0000001210206047_p171491011171512"></a><a name="zh-cn_topic_0000001210206047_p171491011171512"></a>说明</p> 172 </th> 173 </tr> 174 </thead> 175 <tbody><tr id="zh-cn_topic_0000001210206047_row91491211131519"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p914912115156"><a name="zh-cn_topic_0000001210206047_p914912115156"></a><a name="zh-cn_topic_0000001210206047_p914912115156"></a>message</p> 176 </td> 177 <td class="cellrowborder" valign="top" width="23.06%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p1114918116154"><a name="zh-cn_topic_0000001210206047_p1114918116154"></a><a name="zh-cn_topic_0000001210206047_p1114918116154"></a>Object</p> 178 </td> 179 <td class="cellrowborder" valign="top" width="10.870000000000001%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p314916118154"><a name="zh-cn_topic_0000001210206047_p314916118154"></a><a name="zh-cn_topic_0000001210206047_p314916118154"></a>是</p> 180 </td> 181 <td class="cellrowborder" valign="top" width="51.24999999999999%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p814951171516"><a name="zh-cn_topic_0000001210206047_p814951171516"></a><a name="zh-cn_topic_0000001210206047_p814951171516"></a>发送至worker线程的数据。</p> 182 </td> 183 </tr> 184 <tr id="zh-cn_topic_0000001210206047_row5149511191511"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p161491511151511"><a name="zh-cn_topic_0000001210206047_p161491511151511"></a><a name="zh-cn_topic_0000001210206047_p161491511151511"></a>options</p> 185 </td> 186 <td class="cellrowborder" valign="top" width="23.06%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p858904361718"><a name="zh-cn_topic_0000001210206047_p858904361718"></a><a name="zh-cn_topic_0000001210206047_p858904361718"></a><a href="#zh-cn_topic_0000001210206047_section176401093245">PostMessageOptions</a></p> 187 </td> 188 <td class="cellrowborder" valign="top" width="10.870000000000001%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p61491811131518"><a name="zh-cn_topic_0000001210206047_p61491811131518"></a><a name="zh-cn_topic_0000001210206047_p61491811131518"></a>否</p> 189 </td> 190 <td class="cellrowborder" valign="top" width="51.24999999999999%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p67490772615"><a name="zh-cn_topic_0000001210206047_p67490772615"></a><a name="zh-cn_topic_0000001210206047_p67490772615"></a>可转移对象是 ArrayBuffer 的实例对象。transferList数组中不可传入null。</p> 191 </td> 192 </tr> 193 </tbody> 194 </table> 195 196- 示例: 197 198 ``` 199 import worker from '@ohos.worker'; 200 const worker = new worker.Worker("workers/worker.js"); 201 worker.postMessage("hello world"); 202 ``` 203 204 205### postMessage<a name="zh-cn_topic_0000001210206047_section167364819220"></a> 206 207postMessage\(message: Object, transfer: ArrayBuffer\[\]\): void 208 209向worker线程发送消息,传输数据是ArrayBuffer。 210 211- 参数: 212 213 <a name="zh-cn_topic_0000001210206047_table16673104819210"></a> 214 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row167444810213"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p167412481125"><a name="zh-cn_topic_0000001210206047_p167412481125"></a><a name="zh-cn_topic_0000001210206047_p167412481125"></a>参数名</p> 215 </th> 216 <th class="cellrowborder" valign="top" width="23.06%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p166741481213"><a name="zh-cn_topic_0000001210206047_p166741481213"></a><a name="zh-cn_topic_0000001210206047_p166741481213"></a>类型</p> 217 </th> 218 <th class="cellrowborder" valign="top" width="10.870000000000001%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p11674114819219"><a name="zh-cn_topic_0000001210206047_p11674114819219"></a><a name="zh-cn_topic_0000001210206047_p11674114819219"></a>必填</p> 219 </th> 220 <th class="cellrowborder" valign="top" width="51.24999999999999%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p467415481215"><a name="zh-cn_topic_0000001210206047_p467415481215"></a><a name="zh-cn_topic_0000001210206047_p467415481215"></a>说明</p> 221 </th> 222 </tr> 223 </thead> 224 <tbody><tr id="zh-cn_topic_0000001210206047_row1367416481228"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p16674748623"><a name="zh-cn_topic_0000001210206047_p16674748623"></a><a name="zh-cn_topic_0000001210206047_p16674748623"></a>message</p> 225 </td> 226 <td class="cellrowborder" valign="top" width="23.06%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p1267494815212"><a name="zh-cn_topic_0000001210206047_p1267494815212"></a><a name="zh-cn_topic_0000001210206047_p1267494815212"></a>Object</p> 227 </td> 228 <td class="cellrowborder" valign="top" width="10.870000000000001%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p17674154819212"><a name="zh-cn_topic_0000001210206047_p17674154819212"></a><a name="zh-cn_topic_0000001210206047_p17674154819212"></a>是</p> 229 </td> 230 <td class="cellrowborder" valign="top" width="51.24999999999999%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p26741948620"><a name="zh-cn_topic_0000001210206047_p26741948620"></a><a name="zh-cn_topic_0000001210206047_p26741948620"></a>发送至worker线程的数据,必须是ArrayBuffer对象。</p> 231 </td> 232 </tr> 233 <tr id="zh-cn_topic_0000001210206047_row186741348625"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p8424104411411"><a name="zh-cn_topic_0000001210206047_p8424104411411"></a><a name="zh-cn_topic_0000001210206047_p8424104411411"></a>transfer</p> 234 </td> 235 <td class="cellrowborder" valign="top" width="23.06%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p46747488212"><a name="zh-cn_topic_0000001210206047_p46747488212"></a><a name="zh-cn_topic_0000001210206047_p46747488212"></a>ArrayBuffer[]</p> 236 </td> 237 <td class="cellrowborder" valign="top" width="10.870000000000001%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1667418484217"><a name="zh-cn_topic_0000001210206047_p1667418484217"></a><a name="zh-cn_topic_0000001210206047_p1667418484217"></a>是</p> 238 </td> 239 <td class="cellrowborder" valign="top" width="51.24999999999999%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p1567518481923"><a name="zh-cn_topic_0000001210206047_p1567518481923"></a><a name="zh-cn_topic_0000001210206047_p1567518481923"></a>需要转移的 ArrayBuffer 的实例对象集合。</p> 240 </td> 241 </tr> 242 </tbody> 243 </table> 244 245- 示例: 246 247 ``` 248 import worker from '@ohos.worker'; 249 const worker = new worker.Worker("workers/worker.js"); 250 var buffer = new ArrayBuffer(8); 251 worker.postMessage(buffer, [buffer]); 252 ``` 253 254 255### on<a name="zh-cn_topic_0000001210206047_section1201327272"></a> 256 257on\(type: string, listener: EventListener\): void 258 259向worker添加一个事件监听。 260 261- 参数: 262 263 <a name="zh-cn_topic_0000001210206047_table7033219276"></a> 264 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row120143262719"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p13115322273"><a name="zh-cn_topic_0000001210206047_p13115322273"></a><a name="zh-cn_topic_0000001210206047_p13115322273"></a>参数名</p> 265 </th> 266 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p911732152715"><a name="zh-cn_topic_0000001210206047_p911732152715"></a><a name="zh-cn_topic_0000001210206047_p911732152715"></a>类型</p> 267 </th> 268 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p15183232715"><a name="zh-cn_topic_0000001210206047_p15183232715"></a><a name="zh-cn_topic_0000001210206047_p15183232715"></a>必填</p> 269 </th> 270 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p181132192710"><a name="zh-cn_topic_0000001210206047_p181132192710"></a><a name="zh-cn_topic_0000001210206047_p181132192710"></a>说明</p> 271 </th> 272 </tr> 273 </thead> 274 <tbody><tr id="zh-cn_topic_0000001210206047_row161532162715"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p101732182712"><a name="zh-cn_topic_0000001210206047_p101732182712"></a><a name="zh-cn_topic_0000001210206047_p101732182712"></a>type</p> 275 </td> 276 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p2115329277"><a name="zh-cn_topic_0000001210206047_p2115329277"></a><a name="zh-cn_topic_0000001210206047_p2115329277"></a>string</p> 277 </td> 278 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p17123242710"><a name="zh-cn_topic_0000001210206047_p17123242710"></a><a name="zh-cn_topic_0000001210206047_p17123242710"></a>是</p> 279 </td> 280 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p6143272720"><a name="zh-cn_topic_0000001210206047_p6143272720"></a><a name="zh-cn_topic_0000001210206047_p6143272720"></a>监听事件的type。</p> 281 </td> 282 </tr> 283 <tr id="zh-cn_topic_0000001210206047_row3193202715"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p6115326275"><a name="zh-cn_topic_0000001210206047_p6115326275"></a><a name="zh-cn_topic_0000001210206047_p6115326275"></a>listener</p> 284 </td> 285 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p171183272714"><a name="zh-cn_topic_0000001210206047_p171183272714"></a><a name="zh-cn_topic_0000001210206047_p171183272714"></a><a href="#zh-cn_topic_0000001210206047_section53411747134914">EventListener</a></p> 286 </td> 287 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p4183292710"><a name="zh-cn_topic_0000001210206047_p4183292710"></a><a name="zh-cn_topic_0000001210206047_p4183292710"></a>是</p> 288 </td> 289 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p5173202714"><a name="zh-cn_topic_0000001210206047_p5173202714"></a><a name="zh-cn_topic_0000001210206047_p5173202714"></a>回调的事件。</p> 290 </td> 291 </tr> 292 </tbody> 293 </table> 294 295- 示例: 296 297 ``` 298 import worker from '@ohos.worker'; 299 const worker = new worker.Worker("workers/worker.js"); 300 worker.on("alert", (e)=>{ 301 console.log("alert listener callback); 302 }) 303 ``` 304 305 306### once<a name="zh-cn_topic_0000001210206047_section74391240133513"></a> 307 308once\(type: string, listener: EventListener\): void 309 310向worker添加一个事件监听,事件监听只执行一次便自动删除。 311 312- 参数: 313 314 <a name="zh-cn_topic_0000001210206047_table143916408357"></a> 315 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row16439440193511"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p1743974013513"><a name="zh-cn_topic_0000001210206047_p1743974013513"></a><a name="zh-cn_topic_0000001210206047_p1743974013513"></a>参数名</p> 316 </th> 317 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p543924014354"><a name="zh-cn_topic_0000001210206047_p543924014354"></a><a name="zh-cn_topic_0000001210206047_p543924014354"></a>类型</p> 318 </th> 319 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p0439114073513"><a name="zh-cn_topic_0000001210206047_p0439114073513"></a><a name="zh-cn_topic_0000001210206047_p0439114073513"></a>必填</p> 320 </th> 321 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p34398400359"><a name="zh-cn_topic_0000001210206047_p34398400359"></a><a name="zh-cn_topic_0000001210206047_p34398400359"></a>说明</p> 322 </th> 323 </tr> 324 </thead> 325 <tbody><tr id="zh-cn_topic_0000001210206047_row843916409354"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p7439840163518"><a name="zh-cn_topic_0000001210206047_p7439840163518"></a><a name="zh-cn_topic_0000001210206047_p7439840163518"></a>type</p> 326 </td> 327 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p1144044063511"><a name="zh-cn_topic_0000001210206047_p1144044063511"></a><a name="zh-cn_topic_0000001210206047_p1144044063511"></a>string</p> 328 </td> 329 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p15440194023510"><a name="zh-cn_topic_0000001210206047_p15440194023510"></a><a name="zh-cn_topic_0000001210206047_p15440194023510"></a>是</p> 330 </td> 331 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p14401409359"><a name="zh-cn_topic_0000001210206047_p14401409359"></a><a name="zh-cn_topic_0000001210206047_p14401409359"></a>监听事件的type。</p> 332 </td> 333 </tr> 334 <tr id="zh-cn_topic_0000001210206047_row4440040103517"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p644034018354"><a name="zh-cn_topic_0000001210206047_p644034018354"></a><a name="zh-cn_topic_0000001210206047_p644034018354"></a>listener</p> 335 </td> 336 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p144401640163510"><a name="zh-cn_topic_0000001210206047_p144401640163510"></a><a name="zh-cn_topic_0000001210206047_p144401640163510"></a><a href="#zh-cn_topic_0000001210206047_section53411747134914">EventListener</a></p> 337 </td> 338 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p44401740163511"><a name="zh-cn_topic_0000001210206047_p44401740163511"></a><a name="zh-cn_topic_0000001210206047_p44401740163511"></a>是</p> 339 </td> 340 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p1944074073514"><a name="zh-cn_topic_0000001210206047_p1944074073514"></a><a name="zh-cn_topic_0000001210206047_p1944074073514"></a>回调的事件。</p> 341 </td> 342 </tr> 343 </tbody> 344 </table> 345 346- 示例: 347 348 ``` 349 import worker from '@ohos.worker'; 350 const worker = new worker.Worker("workers/worker.js"); 351 worker.once("alert", (e)=>{ 352 console.log("alert listener callback); 353 }) 354 ``` 355 356 357### off<a name="zh-cn_topic_0000001210206047_section8902182918363"></a> 358 359off\(type: string, listener?: EventListener\): void 360 361删除worker的事件监听。 362 363- 参数: 364 365 <a name="zh-cn_topic_0000001210206047_table190342913364"></a> 366 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row11903172973618"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p1690342920362"><a name="zh-cn_topic_0000001210206047_p1690342920362"></a><a name="zh-cn_topic_0000001210206047_p1690342920362"></a>参数名</p> 367 </th> 368 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p1490382916367"><a name="zh-cn_topic_0000001210206047_p1490382916367"></a><a name="zh-cn_topic_0000001210206047_p1490382916367"></a>类型</p> 369 </th> 370 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p99037299364"><a name="zh-cn_topic_0000001210206047_p99037299364"></a><a name="zh-cn_topic_0000001210206047_p99037299364"></a>必填</p> 371 </th> 372 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p1690372953611"><a name="zh-cn_topic_0000001210206047_p1690372953611"></a><a name="zh-cn_topic_0000001210206047_p1690372953611"></a>说明</p> 373 </th> 374 </tr> 375 </thead> 376 <tbody><tr id="zh-cn_topic_0000001210206047_row2090362933610"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p1190462923619"><a name="zh-cn_topic_0000001210206047_p1190462923619"></a><a name="zh-cn_topic_0000001210206047_p1190462923619"></a>type</p> 377 </td> 378 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p12904192913367"><a name="zh-cn_topic_0000001210206047_p12904192913367"></a><a name="zh-cn_topic_0000001210206047_p12904192913367"></a>string</p> 379 </td> 380 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p090462973617"><a name="zh-cn_topic_0000001210206047_p090462973617"></a><a name="zh-cn_topic_0000001210206047_p090462973617"></a>是</p> 381 </td> 382 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p20904122918367"><a name="zh-cn_topic_0000001210206047_p20904122918367"></a><a name="zh-cn_topic_0000001210206047_p20904122918367"></a>需要删除事件的type。</p> 383 </td> 384 </tr> 385 <tr id="zh-cn_topic_0000001210206047_row690452973612"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p12904329163616"><a name="zh-cn_topic_0000001210206047_p12904329163616"></a><a name="zh-cn_topic_0000001210206047_p12904329163616"></a>listener</p> 386 </td> 387 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p2904429173617"><a name="zh-cn_topic_0000001210206047_p2904429173617"></a><a name="zh-cn_topic_0000001210206047_p2904429173617"></a><a href="#zh-cn_topic_0000001210206047_section53411747134914">EventListener</a></p> 388 </td> 389 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p6904152916366"><a name="zh-cn_topic_0000001210206047_p6904152916366"></a><a name="zh-cn_topic_0000001210206047_p6904152916366"></a>否</p> 390 </td> 391 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p29042297361"><a name="zh-cn_topic_0000001210206047_p29042297361"></a><a name="zh-cn_topic_0000001210206047_p29042297361"></a>需要删除的回调的事件。</p> 392 </td> 393 </tr> 394 </tbody> 395 </table> 396 397- 示例: 398 399 ``` 400 import worker from '@ohos.worker'; 401 const worker = new worker.Worker("workers/worker.js"); 402 worker.off("alert"); 403 ``` 404 405 406### terminate<a name="zh-cn_topic_0000001210206047_section17586211104214"></a> 407 408terminate\(\): void 409 410关闭worker线程,终止worker接收消息。 411 412- 示例: 413 414 ``` 415 import worker from '@ohos.worker'; 416 const worker = new worker.Worker("workers/worker.js"); 417 worker.terminate(); 418 ``` 419 420 421### onexit<a name="zh-cn_topic_0000001210206047_section1478913292320"></a> 422 423onexit?: \(code: number\) =\> void 424 425Worker对象的onexit属性表示worker退出时被调用的事件处理程序,处理程序在宿主线程中执行。 426 427- 参数: 428 429 <a name="zh-cn_topic_0000001210206047_table144251138562"></a> 430 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row194251138269"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p1642515387612"><a name="zh-cn_topic_0000001210206047_p1642515387612"></a><a name="zh-cn_topic_0000001210206047_p1642515387612"></a>参数名</p> 431 </th> 432 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p542513381612"><a name="zh-cn_topic_0000001210206047_p542513381612"></a><a name="zh-cn_topic_0000001210206047_p542513381612"></a>类型</p> 433 </th> 434 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p1642513812611"><a name="zh-cn_topic_0000001210206047_p1642513812611"></a><a name="zh-cn_topic_0000001210206047_p1642513812611"></a>必填</p> 435 </th> 436 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p0425133812610"><a name="zh-cn_topic_0000001210206047_p0425133812610"></a><a name="zh-cn_topic_0000001210206047_p0425133812610"></a>说明</p> 437 </th> 438 </tr> 439 </thead> 440 <tbody><tr id="zh-cn_topic_0000001210206047_row1742512381861"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p24258382612"><a name="zh-cn_topic_0000001210206047_p24258382612"></a><a name="zh-cn_topic_0000001210206047_p24258382612"></a>code</p> 441 </td> 442 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p174261438564"><a name="zh-cn_topic_0000001210206047_p174261438564"></a><a name="zh-cn_topic_0000001210206047_p174261438564"></a>number</p> 443 </td> 444 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1842612383620"><a name="zh-cn_topic_0000001210206047_p1842612383620"></a><a name="zh-cn_topic_0000001210206047_p1842612383620"></a>否</p> 445 </td> 446 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p1242614381868"><a name="zh-cn_topic_0000001210206047_p1242614381868"></a><a name="zh-cn_topic_0000001210206047_p1242614381868"></a>worker退出的code。</p> 447 </td> 448 </tr> 449 </tbody> 450 </table> 451 452- 示例: 453 454 ``` 455 import worker from '@ohos.worker'; 456 const worker = new worker.Worker("workers/worker.js"); 457 worker.onexit = function(e) { 458 console.log("onexit"); 459 } 460 ``` 461 462 463### onerror<a name="zh-cn_topic_0000001210206047_section89811422210"></a> 464 465onerror?: \(err: ErrorEvent\) =\> void 466 467Worker对象的onerror属性表示worker在执行过程中发生异常被调用的事件处理程序,处理程序在宿主线程中执行。 468 469- 参数: 470 471 <a name="zh-cn_topic_0000001210206047_table69831416228"></a> 472 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row8989145222"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p1298181418228"><a name="zh-cn_topic_0000001210206047_p1298181418228"></a><a name="zh-cn_topic_0000001210206047_p1298181418228"></a>参数名</p> 473 </th> 474 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p49801492217"><a name="zh-cn_topic_0000001210206047_p49801492217"></a><a name="zh-cn_topic_0000001210206047_p49801492217"></a>类型</p> 475 </th> 476 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p189810140222"><a name="zh-cn_topic_0000001210206047_p189810140222"></a><a name="zh-cn_topic_0000001210206047_p189810140222"></a>必填</p> 477 </th> 478 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p1899131418229"><a name="zh-cn_topic_0000001210206047_p1899131418229"></a><a name="zh-cn_topic_0000001210206047_p1899131418229"></a>说明</p> 479 </th> 480 </tr> 481 </thead> 482 <tbody><tr id="zh-cn_topic_0000001210206047_row699191413222"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p09919145226"><a name="zh-cn_topic_0000001210206047_p09919145226"></a><a name="zh-cn_topic_0000001210206047_p09919145226"></a>err</p> 483 </td> 484 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p1099214172218"><a name="zh-cn_topic_0000001210206047_p1099214172218"></a><a name="zh-cn_topic_0000001210206047_p1099214172218"></a><a href="#zh-cn_topic_0000001210206047_section11241623172418">ErrorEvent</a></p> 485 </td> 486 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1199111442211"><a name="zh-cn_topic_0000001210206047_p1199111442211"></a><a name="zh-cn_topic_0000001210206047_p1199111442211"></a>否</p> 487 </td> 488 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p89991432215"><a name="zh-cn_topic_0000001210206047_p89991432215"></a><a name="zh-cn_topic_0000001210206047_p89991432215"></a>异常数据。</p> 489 </td> 490 </tr> 491 </tbody> 492 </table> 493 494- 示例: 495 496 ``` 497 import worker from '@ohos.worker'; 498 const worker = new worker.Worker("workers/worker.js"); 499 worker.onerror = function(e) { 500 console.log("onerror"); 501 } 502 ``` 503 504 505### onmessage<a name="zh-cn_topic_0000001210206047_section516520044515"></a> 506 507onmessage?: \(event: MessageEvent\) =\> void 508 509Worker对象的onmessage属性表示宿主线程接收到来自其创建的worker通过parentPort.postMessage接口发送的消息时被调用的事件处理程序,处理程序在宿主线程中执行。 510 511- 参数: 512 513 <a name="zh-cn_topic_0000001210206047_table216615019451"></a> 514 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row416618034516"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p17166180154519"><a name="zh-cn_topic_0000001210206047_p17166180154519"></a><a name="zh-cn_topic_0000001210206047_p17166180154519"></a>参数名</p> 515 </th> 516 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p7166605458"><a name="zh-cn_topic_0000001210206047_p7166605458"></a><a name="zh-cn_topic_0000001210206047_p7166605458"></a>类型</p> 517 </th> 518 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p1916610174515"><a name="zh-cn_topic_0000001210206047_p1916610174515"></a><a name="zh-cn_topic_0000001210206047_p1916610174515"></a>必填</p> 519 </th> 520 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p516610024519"><a name="zh-cn_topic_0000001210206047_p516610024519"></a><a name="zh-cn_topic_0000001210206047_p516610024519"></a>说明</p> 521 </th> 522 </tr> 523 </thead> 524 <tbody><tr id="zh-cn_topic_0000001210206047_row10166407457"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p61667010455"><a name="zh-cn_topic_0000001210206047_p61667010455"></a><a name="zh-cn_topic_0000001210206047_p61667010455"></a>event</p> 525 </td> 526 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p4166170164515"><a name="zh-cn_topic_0000001210206047_p4166170164515"></a><a name="zh-cn_topic_0000001210206047_p4166170164515"></a><a href="#zh-cn_topic_0000001210206047_section191271455124710">MessageEvent</a></p> 527 </td> 528 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p016719074513"><a name="zh-cn_topic_0000001210206047_p016719074513"></a><a name="zh-cn_topic_0000001210206047_p016719074513"></a>否</p> 529 </td> 530 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p816770194518"><a name="zh-cn_topic_0000001210206047_p816770194518"></a><a name="zh-cn_topic_0000001210206047_p816770194518"></a>收到的来自worker线程的消息数据。</p> 531 </td> 532 </tr> 533 </tbody> 534 </table> 535 536- 示例: 537 538 ``` 539 import worker from '@ohos.worker'; 540 const worker = new worker.Worker("workers/worker.js"); 541 worker.onmessage = function(e) { 542 console.log("onerror"); 543 } 544 ``` 545 546 547### onmessageerror<a name="zh-cn_topic_0000001210206047_section85981145102714"></a> 548 549onmessageerror?: \(event: MessageEvent\) =\> void 550 551Worker对象的onmessageerror属性表示当 Worker 对象接收到一条无法被序列化的消息时被调用的事件处理程序,处理程序在宿主线程中执行。 552 553- 参数: 554 555 <a name="zh-cn_topic_0000001210206047_table195981745202716"></a> 556 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row2059984512271"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p14599174562718"><a name="zh-cn_topic_0000001210206047_p14599174562718"></a><a name="zh-cn_topic_0000001210206047_p14599174562718"></a>参数名</p> 557 </th> 558 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p135991645152710"><a name="zh-cn_topic_0000001210206047_p135991645152710"></a><a name="zh-cn_topic_0000001210206047_p135991645152710"></a>类型</p> 559 </th> 560 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p1159924522712"><a name="zh-cn_topic_0000001210206047_p1159924522712"></a><a name="zh-cn_topic_0000001210206047_p1159924522712"></a>必填</p> 561 </th> 562 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p8599194522717"><a name="zh-cn_topic_0000001210206047_p8599194522717"></a><a name="zh-cn_topic_0000001210206047_p8599194522717"></a>说明</p> 563 </th> 564 </tr> 565 </thead> 566 <tbody><tr id="zh-cn_topic_0000001210206047_row5599145102715"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p45991645172719"><a name="zh-cn_topic_0000001210206047_p45991645172719"></a><a name="zh-cn_topic_0000001210206047_p45991645172719"></a>event</p> 567 </td> 568 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p135991645122712"><a name="zh-cn_topic_0000001210206047_p135991645122712"></a><a name="zh-cn_topic_0000001210206047_p135991645122712"></a><a href="#zh-cn_topic_0000001210206047_section191271455124710">MessageEvent</a></p> 569 </td> 570 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p18599134519278"><a name="zh-cn_topic_0000001210206047_p18599134519278"></a><a name="zh-cn_topic_0000001210206047_p18599134519278"></a>否</p> 571 </td> 572 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p10600134514271"><a name="zh-cn_topic_0000001210206047_p10600134514271"></a><a name="zh-cn_topic_0000001210206047_p10600134514271"></a>异常数据。</p> 573 </td> 574 </tr> 575 </tbody> 576 </table> 577 578- 示例: 579 580 ``` 581 import worker from '@ohos.worker'; 582 const worker = new worker.Worker("workers/worker.js"); 583 worker.onmessageerror= function(e) { 584 console.log("onmessageerror"); 585 } 586 ``` 587 588 589## EventTarget<a name="zh-cn_topic_0000001210206047_section3283104561014"></a> 590 591### addEventListener<a name="zh-cn_topic_0000001210206047_section16405103834511"></a> 592 593addEventListener\(type: string, listener: EventListener\): void 594 595向worker添加一个事件监听。 596 597- 参数: 598 599 <a name="zh-cn_topic_0000001210206047_table12405138134519"></a> 600 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row1040553815452"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p1040523804519"><a name="zh-cn_topic_0000001210206047_p1040523804519"></a><a name="zh-cn_topic_0000001210206047_p1040523804519"></a>参数名</p> 601 </th> 602 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p740553818457"><a name="zh-cn_topic_0000001210206047_p740553818457"></a><a name="zh-cn_topic_0000001210206047_p740553818457"></a>类型</p> 603 </th> 604 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p11405183814513"><a name="zh-cn_topic_0000001210206047_p11405183814513"></a><a name="zh-cn_topic_0000001210206047_p11405183814513"></a>必填</p> 605 </th> 606 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p54066385459"><a name="zh-cn_topic_0000001210206047_p54066385459"></a><a name="zh-cn_topic_0000001210206047_p54066385459"></a>说明</p> 607 </th> 608 </tr> 609 </thead> 610 <tbody><tr id="zh-cn_topic_0000001210206047_row94061238174510"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p194061438164512"><a name="zh-cn_topic_0000001210206047_p194061438164512"></a><a name="zh-cn_topic_0000001210206047_p194061438164512"></a>type</p> 611 </td> 612 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p8406163864513"><a name="zh-cn_topic_0000001210206047_p8406163864513"></a><a name="zh-cn_topic_0000001210206047_p8406163864513"></a>string</p> 613 </td> 614 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p4406173816452"><a name="zh-cn_topic_0000001210206047_p4406173816452"></a><a name="zh-cn_topic_0000001210206047_p4406173816452"></a>是</p> 615 </td> 616 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p440603894519"><a name="zh-cn_topic_0000001210206047_p440603894519"></a><a name="zh-cn_topic_0000001210206047_p440603894519"></a>监听事件的type。</p> 617 </td> 618 </tr> 619 <tr id="zh-cn_topic_0000001210206047_row1840693864519"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p1140603814450"><a name="zh-cn_topic_0000001210206047_p1140603814450"></a><a name="zh-cn_topic_0000001210206047_p1140603814450"></a>listener</p> 620 </td> 621 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p104061338184515"><a name="zh-cn_topic_0000001210206047_p104061338184515"></a><a name="zh-cn_topic_0000001210206047_p104061338184515"></a><a href="#zh-cn_topic_0000001210206047_section53411747134914">EventListener</a></p> 622 </td> 623 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1040663812451"><a name="zh-cn_topic_0000001210206047_p1040663812451"></a><a name="zh-cn_topic_0000001210206047_p1040663812451"></a>是</p> 624 </td> 625 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p19406113874515"><a name="zh-cn_topic_0000001210206047_p19406113874515"></a><a name="zh-cn_topic_0000001210206047_p19406113874515"></a>回调的事件。</p> 626 </td> 627 </tr> 628 </tbody> 629 </table> 630 631- 示例: 632 633 ``` 634 import worker from '@ohos.worker'; 635 const worker = new worker.Worker("workers/worker.js"); 636 worker.addEventListener("alert", (e)=>{ 637 console.log("alert listener callback); 638 }) 639 ``` 640 641 642### removeEventListener<a name="zh-cn_topic_0000001210206047_section1437219413469"></a> 643 644removeEventListener\(type: string, callback?: EventListener\): void 645 646删除worker的事件监听。 647 648- 参数: 649 650 <a name="zh-cn_topic_0000001210206047_table53725413461"></a> 651 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row837284174614"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p1637216414614"><a name="zh-cn_topic_0000001210206047_p1637216414614"></a><a name="zh-cn_topic_0000001210206047_p1637216414614"></a>参数名</p> 652 </th> 653 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p1037220454619"><a name="zh-cn_topic_0000001210206047_p1037220454619"></a><a name="zh-cn_topic_0000001210206047_p1037220454619"></a>类型</p> 654 </th> 655 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p2372547464"><a name="zh-cn_topic_0000001210206047_p2372547464"></a><a name="zh-cn_topic_0000001210206047_p2372547464"></a>必填</p> 656 </th> 657 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p1937217484617"><a name="zh-cn_topic_0000001210206047_p1937217484617"></a><a name="zh-cn_topic_0000001210206047_p1937217484617"></a>说明</p> 658 </th> 659 </tr> 660 </thead> 661 <tbody><tr id="zh-cn_topic_0000001210206047_row437224104618"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p193735414619"><a name="zh-cn_topic_0000001210206047_p193735414619"></a><a name="zh-cn_topic_0000001210206047_p193735414619"></a>type</p> 662 </td> 663 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p63731542468"><a name="zh-cn_topic_0000001210206047_p63731542468"></a><a name="zh-cn_topic_0000001210206047_p63731542468"></a>string</p> 664 </td> 665 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1437320413469"><a name="zh-cn_topic_0000001210206047_p1437320413469"></a><a name="zh-cn_topic_0000001210206047_p1437320413469"></a>是</p> 666 </td> 667 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p23739494618"><a name="zh-cn_topic_0000001210206047_p23739494618"></a><a name="zh-cn_topic_0000001210206047_p23739494618"></a>需要删除事件的type。</p> 668 </td> 669 </tr> 670 <tr id="zh-cn_topic_0000001210206047_row8373174114617"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p43731644464"><a name="zh-cn_topic_0000001210206047_p43731644464"></a><a name="zh-cn_topic_0000001210206047_p43731644464"></a>callback</p> 671 </td> 672 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p1737324114614"><a name="zh-cn_topic_0000001210206047_p1737324114614"></a><a name="zh-cn_topic_0000001210206047_p1737324114614"></a><a href="#zh-cn_topic_0000001210206047_section53411747134914">EventListener</a></p> 673 </td> 674 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p0373243465"><a name="zh-cn_topic_0000001210206047_p0373243465"></a><a name="zh-cn_topic_0000001210206047_p0373243465"></a>否</p> 675 </td> 676 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p132381754719"><a name="zh-cn_topic_0000001210206047_p132381754719"></a><a name="zh-cn_topic_0000001210206047_p132381754719"></a>需要删除的回调的事件。</p> 677 </td> 678 </tr> 679 </tbody> 680 </table> 681 682- 示例: 683 684 ``` 685 import worker from '@ohos.worker'; 686 const worker = new worker.Worker("workers/worker.js"); 687 worker.removeEventListener("alert"); 688 ``` 689 690 691### dispatchEvent<a name="zh-cn_topic_0000001210206047_section1593912134916"></a> 692 693dispatchEvent\(event: Event\): boolean 694 695分发定义在worker的事件。 696 697- 参数: 698 699 <a name="zh-cn_topic_0000001210206047_table859431224913"></a> 700 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row859451213495"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p11594111215495"><a name="zh-cn_topic_0000001210206047_p11594111215495"></a><a name="zh-cn_topic_0000001210206047_p11594111215495"></a>参数名</p> 701 </th> 702 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p175946122494"><a name="zh-cn_topic_0000001210206047_p175946122494"></a><a name="zh-cn_topic_0000001210206047_p175946122494"></a>类型</p> 703 </th> 704 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p85941912124919"><a name="zh-cn_topic_0000001210206047_p85941912124919"></a><a name="zh-cn_topic_0000001210206047_p85941912124919"></a>必填</p> 705 </th> 706 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p2059551219493"><a name="zh-cn_topic_0000001210206047_p2059551219493"></a><a name="zh-cn_topic_0000001210206047_p2059551219493"></a>说明</p> 707 </th> 708 </tr> 709 </thead> 710 <tbody><tr id="zh-cn_topic_0000001210206047_row45951812184915"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p11595612144910"><a name="zh-cn_topic_0000001210206047_p11595612144910"></a><a name="zh-cn_topic_0000001210206047_p11595612144910"></a>event</p> 711 </td> 712 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p17595712184913"><a name="zh-cn_topic_0000001210206047_p17595712184913"></a><a name="zh-cn_topic_0000001210206047_p17595712184913"></a><a href="#zh-cn_topic_0000001210206047_section1674694018507">Event</a></p> 713 </td> 714 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p20595101217490"><a name="zh-cn_topic_0000001210206047_p20595101217490"></a><a name="zh-cn_topic_0000001210206047_p20595101217490"></a>是</p> 715 </td> 716 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p18595712144914"><a name="zh-cn_topic_0000001210206047_p18595712144914"></a><a name="zh-cn_topic_0000001210206047_p18595712144914"></a>需要分发的事件。</p> 717 </td> 718 </tr> 719 </tbody> 720 </table> 721 722- 返回值: 723 724 <a name="zh-cn_topic_0000001210206047_table4536208105515"></a> 725 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row25360810551"><th class="cellrowborder" valign="top" width="20.990000000000002%" id="mcps1.1.3.1.1"><p id="zh-cn_topic_0000001210206047_p2536685552"><a name="zh-cn_topic_0000001210206047_p2536685552"></a><a name="zh-cn_topic_0000001210206047_p2536685552"></a>参数名</p> 726 </th> 727 <th class="cellrowborder" valign="top" width="79.01%" id="mcps1.1.3.1.2"><p id="zh-cn_topic_0000001210206047_p185371287554"><a name="zh-cn_topic_0000001210206047_p185371287554"></a><a name="zh-cn_topic_0000001210206047_p185371287554"></a>说明</p> 728 </th> 729 </tr> 730 </thead> 731 <tbody><tr id="zh-cn_topic_0000001210206047_row55371387555"><td class="cellrowborder" valign="top" width="20.990000000000002%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0000001210206047_p353718195511"><a name="zh-cn_topic_0000001210206047_p353718195511"></a><a name="zh-cn_topic_0000001210206047_p353718195511"></a>boolean</p> 732 </td> 733 <td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0000001210206047_p165388817555"><a name="zh-cn_topic_0000001210206047_p165388817555"></a><a name="zh-cn_topic_0000001210206047_p165388817555"></a>分发的结果,false表示分发失败。</p> 734 </td> 735 </tr> 736 </tbody> 737 </table> 738 739- 示例: 740 741 ``` 742 import worker from '@ohos.worker'; 743 const worker = new worker.Worker("workers/worker.js"); 744 worker.dispatchEvent({type:"alert"}); 745 ``` 746 747 748### removeAllListener<a name="zh-cn_topic_0000001210206047_section1795215505533"></a> 749 750removeAllListener\(\): void 751 752删除worker的所有事件监听。 753 754- 示例: 755 756 ``` 757 import worker from '@ohos.worker'; 758 const worker = new worker.Worker("workers/worker.js"); 759 worker.removeAllListener(); 760 ``` 761 762 763## DedicatedWorkerGlobalScope<a name="zh-cn_topic_0000001210206047_section36872023123912"></a> 764 765worker线程用于与宿主线程通信的类,通过postMessage接口发送消息给宿主线程、close接口关闭worker线程,DedicatedWorkerGlobalScope类继承[WorkerGlobalScope](#zh-cn_topic_0000001210206047_section12882825611)。 766 767### postMessage<a name="zh-cn_topic_0000001210206047_section1570345794013"></a> 768 769postMessage\(messageObject: Object, options?: PostMessageOptions\): void 770 771worker向宿主线程发送消息。 772 773- 参数: 774 775 <a name="zh-cn_topic_0000001210206047_table137041557154020"></a> 776 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row97049574404"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p0704165764012"><a name="zh-cn_topic_0000001210206047_p0704165764012"></a><a name="zh-cn_topic_0000001210206047_p0704165764012"></a>参数名</p> 777 </th> 778 <th class="cellrowborder" valign="top" width="25.869999999999997%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p1570465764014"><a name="zh-cn_topic_0000001210206047_p1570465764014"></a><a name="zh-cn_topic_0000001210206047_p1570465764014"></a>类型</p> 779 </th> 780 <th class="cellrowborder" valign="top" width="9.39%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p15704157104011"><a name="zh-cn_topic_0000001210206047_p15704157104011"></a><a name="zh-cn_topic_0000001210206047_p15704157104011"></a>必填</p> 781 </th> 782 <th class="cellrowborder" valign="top" width="49.919999999999995%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p6704115794013"><a name="zh-cn_topic_0000001210206047_p6704115794013"></a><a name="zh-cn_topic_0000001210206047_p6704115794013"></a>说明</p> 783 </th> 784 </tr> 785 </thead> 786 <tbody><tr id="zh-cn_topic_0000001210206047_row11704165784014"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p16704357174017"><a name="zh-cn_topic_0000001210206047_p16704357174017"></a><a name="zh-cn_topic_0000001210206047_p16704357174017"></a>messageObject</p> 787 </td> 788 <td class="cellrowborder" valign="top" width="25.869999999999997%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p970435754019"><a name="zh-cn_topic_0000001210206047_p970435754019"></a><a name="zh-cn_topic_0000001210206047_p970435754019"></a>Object</p> 789 </td> 790 <td class="cellrowborder" valign="top" width="9.39%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1670405744010"><a name="zh-cn_topic_0000001210206047_p1670405744010"></a><a name="zh-cn_topic_0000001210206047_p1670405744010"></a>是</p> 791 </td> 792 <td class="cellrowborder" valign="top" width="49.919999999999995%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p10704457184013"><a name="zh-cn_topic_0000001210206047_p10704457184013"></a><a name="zh-cn_topic_0000001210206047_p10704457184013"></a>发送至宿主线程的数据。</p> 793 </td> 794 </tr> 795 <tr id="zh-cn_topic_0000001210206047_row15704957194018"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p3705125754018"><a name="zh-cn_topic_0000001210206047_p3705125754018"></a><a name="zh-cn_topic_0000001210206047_p3705125754018"></a>options</p> 796 </td> 797 <td class="cellrowborder" valign="top" width="25.869999999999997%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p19705125716402"><a name="zh-cn_topic_0000001210206047_p19705125716402"></a><a name="zh-cn_topic_0000001210206047_p19705125716402"></a><a href="#zh-cn_topic_0000001210206047_section176401093245">PostMessageOptions</a></p> 798 </td> 799 <td class="cellrowborder" valign="top" width="9.39%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1570535784016"><a name="zh-cn_topic_0000001210206047_p1570535784016"></a><a name="zh-cn_topic_0000001210206047_p1570535784016"></a>否</p> 800 </td> 801 <td class="cellrowborder" valign="top" width="49.919999999999995%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p8705857154016"><a name="zh-cn_topic_0000001210206047_p8705857154016"></a><a name="zh-cn_topic_0000001210206047_p8705857154016"></a>可转移对象是ArrayBuffer的实例对象。transferList数组中不可传入null。</p> 802 </td> 803 </tr> 804 </tbody> 805 </table> 806 807- 示例: 808 809 ``` 810 main.js 811 import worker from '@ohos.worker'; 812 const worker = new worker.Worker("workers/worker.js"); 813 worker.onmessage = function(e) { 814 console.log("receive data from worker.js"); 815 } 816 worker.postMessage("hello world"); 817 818 worker.js 819 import worker from "@ohos.worker"; 820 const parentPort = worker.parentPort; 821 parentPort.onmessage = function(e){ 822 parentPort.postMessage("receive data from main.js"); 823 } 824 ``` 825 826 827### postMessage<a name="zh-cn_topic_0000001210206047_section7211194212107"></a> 828 829postMessage\(messageObject: Object, transfer: Transferable\[\]\): void 830 831worker向宿主线程发送消息。,传输数据是ArrayBuffer。 832 833- 参数: 834 835 <a name="zh-cn_topic_0000001210206047_table1121174210107"></a> 836 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row221115428107"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p1321154291010"><a name="zh-cn_topic_0000001210206047_p1321154291010"></a><a name="zh-cn_topic_0000001210206047_p1321154291010"></a>参数名</p> 837 </th> 838 <th class="cellrowborder" valign="top" width="23.06%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p20211942121016"><a name="zh-cn_topic_0000001210206047_p20211942121016"></a><a name="zh-cn_topic_0000001210206047_p20211942121016"></a>类型</p> 839 </th> 840 <th class="cellrowborder" valign="top" width="10.870000000000001%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p20212204241017"><a name="zh-cn_topic_0000001210206047_p20212204241017"></a><a name="zh-cn_topic_0000001210206047_p20212204241017"></a>必填</p> 841 </th> 842 <th class="cellrowborder" valign="top" width="51.24999999999999%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p1021274261012"><a name="zh-cn_topic_0000001210206047_p1021274261012"></a><a name="zh-cn_topic_0000001210206047_p1021274261012"></a>说明</p> 843 </th> 844 </tr> 845 </thead> 846 <tbody><tr id="zh-cn_topic_0000001210206047_row11212164241011"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p1321294281016"><a name="zh-cn_topic_0000001210206047_p1321294281016"></a><a name="zh-cn_topic_0000001210206047_p1321294281016"></a>messageObject</p> 847 </td> 848 <td class="cellrowborder" valign="top" width="23.06%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p82123427109"><a name="zh-cn_topic_0000001210206047_p82123427109"></a><a name="zh-cn_topic_0000001210206047_p82123427109"></a>Object</p> 849 </td> 850 <td class="cellrowborder" valign="top" width="10.870000000000001%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p15212144220102"><a name="zh-cn_topic_0000001210206047_p15212144220102"></a><a name="zh-cn_topic_0000001210206047_p15212144220102"></a>是</p> 851 </td> 852 <td class="cellrowborder" valign="top" width="51.24999999999999%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p15212124221014"><a name="zh-cn_topic_0000001210206047_p15212124221014"></a><a name="zh-cn_topic_0000001210206047_p15212124221014"></a>发送至宿主线程的数据,必须是ArrayBuffer对象。</p> 853 </td> 854 </tr> 855 <tr id="zh-cn_topic_0000001210206047_row3212742181015"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p821214216104"><a name="zh-cn_topic_0000001210206047_p821214216104"></a><a name="zh-cn_topic_0000001210206047_p821214216104"></a>transfer</p> 856 </td> 857 <td class="cellrowborder" valign="top" width="23.06%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p182123429107"><a name="zh-cn_topic_0000001210206047_p182123429107"></a><a name="zh-cn_topic_0000001210206047_p182123429107"></a>Transferable[]</p> 858 </td> 859 <td class="cellrowborder" valign="top" width="10.870000000000001%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p19212204211011"><a name="zh-cn_topic_0000001210206047_p19212204211011"></a><a name="zh-cn_topic_0000001210206047_p19212204211011"></a>是</p> 860 </td> 861 <td class="cellrowborder" valign="top" width="51.24999999999999%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p152121642141019"><a name="zh-cn_topic_0000001210206047_p152121642141019"></a><a name="zh-cn_topic_0000001210206047_p152121642141019"></a>需要转移的 ArrayBuffer 的实例对象集合。</p> 862 </td> 863 </tr> 864 </tbody> 865 </table> 866 867- 示例: 868 869 ``` 870 main.js 871 import worker from '@ohos.worker'; 872 const worker = new worker.Worker("workers/worker.js"); 873 worker.onmessage = function(e) { 874 console.log("receive data from worker.js"); 875 } 876 worker.postMessage("hello world"); 877 878 worker.js 879 import worker from "@ohos.worker"; 880 const parentPort = worker.parentPort; 881 var buffer = new ArrayBuffer(8); 882 parentPort.onmessage = function(e){ 883 parentPort.postMessage(buffer, [buffer]); 884 } 885 ``` 886 887 888### close<a name="zh-cn_topic_0000001210206047_section849610376427"></a> 889 890close\(\): void 891 892关闭worker线程,终止worker接收消息。 893 894- 示例: 895 896 ``` 897 main.js 898 import worker from '@ohos.worker'; 899 const worker = new worker.Worker("workers/worker.js"); 900 parentPort.postMessage("over"); 901 902 worker.js 903 import worker from "@ohos.worker"; 904 const parentPort = worker.parentPort; 905 parentPort.onmessage = function(e) { 906 parentPort.close(); 907 } 908 ``` 909 910 911### onmessage<a name="zh-cn_topic_0000001210206047_section1371121184815"></a> 912 913onmessage?: \(this: DedicatedWorkerGlobalScope, ev: MessageEvent\) =\> void 914 915DedicatedWorkerGlobalScope的onmessage属性表示worker线程收到来自其宿主线程通过worker.postMessage接口发送的消息时被调用的事件处理程序,处理程序在worker线程中执行。 916 917- 参数: 918 919 <a name="zh-cn_topic_0000001210206047_table137182118481"></a> 920 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row4372182164815"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p2372521144814"><a name="zh-cn_topic_0000001210206047_p2372521144814"></a><a name="zh-cn_topic_0000001210206047_p2372521144814"></a>参数名</p> 921 </th> 922 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p437213213483"><a name="zh-cn_topic_0000001210206047_p437213213483"></a><a name="zh-cn_topic_0000001210206047_p437213213483"></a>类型</p> 923 </th> 924 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p15372182118484"><a name="zh-cn_topic_0000001210206047_p15372182118484"></a><a name="zh-cn_topic_0000001210206047_p15372182118484"></a>必填</p> 925 </th> 926 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p1637282113484"><a name="zh-cn_topic_0000001210206047_p1637282113484"></a><a name="zh-cn_topic_0000001210206047_p1637282113484"></a>说明</p> 927 </th> 928 </tr> 929 </thead> 930 <tbody><tr id="zh-cn_topic_0000001210206047_row994815559196"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p0948955201912"><a name="zh-cn_topic_0000001210206047_p0948955201912"></a><a name="zh-cn_topic_0000001210206047_p0948955201912"></a>this</p> 931 </td> 932 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p894835517191"><a name="zh-cn_topic_0000001210206047_p894835517191"></a><a name="zh-cn_topic_0000001210206047_p894835517191"></a>DedicatedWorkerGlobalScope</p> 933 </td> 934 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p109481255121914"><a name="zh-cn_topic_0000001210206047_p109481255121914"></a><a name="zh-cn_topic_0000001210206047_p109481255121914"></a>否</p> 935 </td> 936 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p1594811551192"><a name="zh-cn_topic_0000001210206047_p1594811551192"></a><a name="zh-cn_topic_0000001210206047_p1594811551192"></a>DedicatedWorkerGlobalScope本身。</p> 937 </td> 938 </tr> 939 <tr id="zh-cn_topic_0000001210206047_row103721521164820"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p13721821124817"><a name="zh-cn_topic_0000001210206047_p13721821124817"></a><a name="zh-cn_topic_0000001210206047_p13721821124817"></a>ev</p> 940 </td> 941 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p16372172144812"><a name="zh-cn_topic_0000001210206047_p16372172144812"></a><a name="zh-cn_topic_0000001210206047_p16372172144812"></a><a href="#zh-cn_topic_0000001210206047_section191271455124710">MessageEvent</a></p> 942 </td> 943 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p1737232117486"><a name="zh-cn_topic_0000001210206047_p1737232117486"></a><a name="zh-cn_topic_0000001210206047_p1737232117486"></a>否</p> 944 </td> 945 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p6372142184820"><a name="zh-cn_topic_0000001210206047_p6372142184820"></a><a name="zh-cn_topic_0000001210206047_p6372142184820"></a>收到的来自宿主线程的消息数据。</p> 946 </td> 947 </tr> 948 </tbody> 949 </table> 950 951- 示例: 952 953 ``` 954 main.js 955 import worker from '@ohos.worker'; 956 const worker = new worker.Worker("workers/worker.js"); 957 worker.postMessage("hello world"); 958 959 worker.js 960 import worker from "@ohos.worker"; 961 const parentPort = worker.parentPort; 962 parentPort.onmessage = function(e) { 963 console.log("receive main.js message"); 964 } 965 ``` 966 967 968### onmessageerror<a name="zh-cn_topic_0000001210206047_section16116749135015"></a> 969 970onmessageerror?: \(this: DedicatedWorkerGlobalScope, ev: MessageEvent\) =\> void 971 972DedicatedWorkerGlobalScope的onmessageerror属性表示当 Worker 对象接收到一条无法被反序列化的消息时被调用的事件处理程序,处理程序在worker线程中执行。 973 974- 参数: 975 976 <a name="zh-cn_topic_0000001210206047_table21169494501"></a> 977 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row1411794917507"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p2117134985012"><a name="zh-cn_topic_0000001210206047_p2117134985012"></a><a name="zh-cn_topic_0000001210206047_p2117134985012"></a>参数名</p> 978 </th> 979 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p161171949165016"><a name="zh-cn_topic_0000001210206047_p161171949165016"></a><a name="zh-cn_topic_0000001210206047_p161171949165016"></a>类型</p> 980 </th> 981 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p5117104915501"><a name="zh-cn_topic_0000001210206047_p5117104915501"></a><a name="zh-cn_topic_0000001210206047_p5117104915501"></a>必填</p> 982 </th> 983 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p111734915011"><a name="zh-cn_topic_0000001210206047_p111734915011"></a><a name="zh-cn_topic_0000001210206047_p111734915011"></a>说明</p> 984 </th> 985 </tr> 986 </thead> 987 <tbody><tr id="zh-cn_topic_0000001210206047_row116191720217"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p362016292115"><a name="zh-cn_topic_0000001210206047_p362016292115"></a><a name="zh-cn_topic_0000001210206047_p362016292115"></a>this</p> 988 </td> 989 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p3620725214"><a name="zh-cn_topic_0000001210206047_p3620725214"></a><a name="zh-cn_topic_0000001210206047_p3620725214"></a>DedicatedWorkerGlobalScope</p> 990 </td> 991 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p4620923218"><a name="zh-cn_topic_0000001210206047_p4620923218"></a><a name="zh-cn_topic_0000001210206047_p4620923218"></a>否</p> 992 </td> 993 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p362018232114"><a name="zh-cn_topic_0000001210206047_p362018232114"></a><a name="zh-cn_topic_0000001210206047_p362018232114"></a>DedicatedWorkerGlobalScope本身。</p> 994 </td> 995 </tr> 996 <tr id="zh-cn_topic_0000001210206047_row311784995013"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p154087331212"><a name="zh-cn_topic_0000001210206047_p154087331212"></a><a name="zh-cn_topic_0000001210206047_p154087331212"></a>ev</p> 997 </td> 998 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p10117154917508"><a name="zh-cn_topic_0000001210206047_p10117154917508"></a><a name="zh-cn_topic_0000001210206047_p10117154917508"></a><a href="#zh-cn_topic_0000001210206047_section191271455124710">MessageEvent</a></p> 999 </td> 1000 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p711744911507"><a name="zh-cn_topic_0000001210206047_p711744911507"></a><a name="zh-cn_topic_0000001210206047_p711744911507"></a>否</p> 1001 </td> 1002 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p201188499500"><a name="zh-cn_topic_0000001210206047_p201188499500"></a><a name="zh-cn_topic_0000001210206047_p201188499500"></a>异常数据。</p> 1003 </td> 1004 </tr> 1005 </tbody> 1006 </table> 1007 1008- 示例: 1009 1010 ``` 1011 main.js 1012 import worker from '@ohos.worker'; 1013 const worker = new worker.Worker("workers/worker.js"); 1014 1015 worker.js 1016 import worker from "@ohos.worker"; 1017 const parentPort = worker.parentPort; 1018 parentPort.onmessageerror= function(e) { 1019 console.log("worker.js onmessageerror"); 1020 } 1021 ``` 1022 1023 1024## PostMessageOptions<a name="zh-cn_topic_0000001210206047_section176401093245"></a> 1025 1026明确数据传递过程中需要转移所有权对象的类,传递所有权的对象必须是ArrayBuffer。 1027 1028<a name="zh-cn_topic_0000001210206047_table1564010972415"></a> 1029<table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row3640129112415"><th class="cellrowborder" valign="top" width="15.870000000000001%" id="mcps1.1.6.1.1"><p id="zh-cn_topic_0000001210206047_p1364089142412"><a name="zh-cn_topic_0000001210206047_p1364089142412"></a><a name="zh-cn_topic_0000001210206047_p1364089142412"></a>名称</p> 1030</th> 1031<th class="cellrowborder" valign="top" width="10.86%" id="mcps1.1.6.1.2"><p id="zh-cn_topic_0000001210206047_p564013912415"><a name="zh-cn_topic_0000001210206047_p564013912415"></a><a name="zh-cn_topic_0000001210206047_p564013912415"></a>参数类型</p> 1032</th> 1033<th class="cellrowborder" valign="top" width="9.34%" id="mcps1.1.6.1.3"><p id="zh-cn_topic_0000001210206047_p1264016913243"><a name="zh-cn_topic_0000001210206047_p1264016913243"></a><a name="zh-cn_topic_0000001210206047_p1264016913243"></a>可读</p> 1034</th> 1035<th class="cellrowborder" valign="top" width="11.51%" id="mcps1.1.6.1.4"><p id="zh-cn_topic_0000001210206047_p1164089172413"><a name="zh-cn_topic_0000001210206047_p1164089172413"></a><a name="zh-cn_topic_0000001210206047_p1164089172413"></a>可写</p> 1036</th> 1037<th class="cellrowborder" valign="top" width="52.42%" id="mcps1.1.6.1.5"><p id="zh-cn_topic_0000001210206047_p7640169172410"><a name="zh-cn_topic_0000001210206047_p7640169172410"></a><a name="zh-cn_topic_0000001210206047_p7640169172410"></a>说明</p> 1038</th> 1039</tr> 1040</thead> 1041<tbody><tr id="zh-cn_topic_0000001210206047_row1064016982410"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p156410962416"><a name="zh-cn_topic_0000001210206047_p156410962416"></a><a name="zh-cn_topic_0000001210206047_p156410962416"></a>transfer</p> 1042</td> 1043<td class="cellrowborder" valign="top" width="10.86%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p59311042152414"><a name="zh-cn_topic_0000001210206047_p59311042152414"></a><a name="zh-cn_topic_0000001210206047_p59311042152414"></a>Object[]</p> 1044</td> 1045<td class="cellrowborder" valign="top" width="9.34%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p17641179102414"><a name="zh-cn_topic_0000001210206047_p17641179102414"></a><a name="zh-cn_topic_0000001210206047_p17641179102414"></a>是</p> 1046</td> 1047<td class="cellrowborder" valign="top" width="11.51%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p1864112919241"><a name="zh-cn_topic_0000001210206047_p1864112919241"></a><a name="zh-cn_topic_0000001210206047_p1864112919241"></a>是</p> 1048</td> 1049<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p18641139162415"><a name="zh-cn_topic_0000001210206047_p18641139162415"></a><a name="zh-cn_topic_0000001210206047_p18641139162415"></a>ArrayBuffer数组,用于传递所有权。</p> 1050</td> 1051</tr> 1052</tbody> 1053</table> 1054 1055## Event<a name="zh-cn_topic_0000001210206047_section1674694018507"></a> 1056 1057事件类。 1058 1059<a name="zh-cn_topic_0000001210206047_table127461440175015"></a> 1060<table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row10746640145010"><th class="cellrowborder" valign="top" width="15.870000000000001%" id="mcps1.1.6.1.1"><p id="zh-cn_topic_0000001210206047_p1174613403508"><a name="zh-cn_topic_0000001210206047_p1174613403508"></a><a name="zh-cn_topic_0000001210206047_p1174613403508"></a>名称</p> 1061</th> 1062<th class="cellrowborder" valign="top" width="13.569999999999999%" id="mcps1.1.6.1.2"><p id="zh-cn_topic_0000001210206047_p1274614055018"><a name="zh-cn_topic_0000001210206047_p1274614055018"></a><a name="zh-cn_topic_0000001210206047_p1274614055018"></a>参数类型</p> 1063</th> 1064<th class="cellrowborder" valign="top" width="9.700000000000001%" id="mcps1.1.6.1.3"><p id="zh-cn_topic_0000001210206047_p37460409501"><a name="zh-cn_topic_0000001210206047_p37460409501"></a><a name="zh-cn_topic_0000001210206047_p37460409501"></a>可读</p> 1065</th> 1066<th class="cellrowborder" valign="top" width="8.44%" id="mcps1.1.6.1.4"><p id="zh-cn_topic_0000001210206047_p137461340145015"><a name="zh-cn_topic_0000001210206047_p137461340145015"></a><a name="zh-cn_topic_0000001210206047_p137461340145015"></a>可写</p> 1067</th> 1068<th class="cellrowborder" valign="top" width="52.42%" id="mcps1.1.6.1.5"><p id="zh-cn_topic_0000001210206047_p874616406508"><a name="zh-cn_topic_0000001210206047_p874616406508"></a><a name="zh-cn_topic_0000001210206047_p874616406508"></a>说明</p> 1069</th> 1070</tr> 1071</thead> 1072<tbody><tr id="zh-cn_topic_0000001210206047_row117475400509"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p1074754095015"><a name="zh-cn_topic_0000001210206047_p1074754095015"></a><a name="zh-cn_topic_0000001210206047_p1074754095015"></a>type</p> 1073</td> 1074<td class="cellrowborder" valign="top" width="13.569999999999999%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p5747240135016"><a name="zh-cn_topic_0000001210206047_p5747240135016"></a><a name="zh-cn_topic_0000001210206047_p5747240135016"></a>string</p> 1075</td> 1076<td class="cellrowborder" valign="top" width="9.700000000000001%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p197471409508"><a name="zh-cn_topic_0000001210206047_p197471409508"></a><a name="zh-cn_topic_0000001210206047_p197471409508"></a>是</p> 1077</td> 1078<td class="cellrowborder" valign="top" width="8.44%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p1974719408501"><a name="zh-cn_topic_0000001210206047_p1974719408501"></a><a name="zh-cn_topic_0000001210206047_p1974719408501"></a>否</p> 1079</td> 1080<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p974715404504"><a name="zh-cn_topic_0000001210206047_p974715404504"></a><a name="zh-cn_topic_0000001210206047_p974715404504"></a>指定事件的type。</p> 1081</td> 1082</tr> 1083<tr id="zh-cn_topic_0000001210206047_row1360594017519"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p3605340165112"><a name="zh-cn_topic_0000001210206047_p3605340165112"></a><a name="zh-cn_topic_0000001210206047_p3605340165112"></a>timeStamp</p> 1084</td> 1085<td class="cellrowborder" valign="top" width="13.569999999999999%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p260516404519"><a name="zh-cn_topic_0000001210206047_p260516404519"></a><a name="zh-cn_topic_0000001210206047_p260516404519"></a>number</p> 1086</td> 1087<td class="cellrowborder" valign="top" width="9.700000000000001%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p1360513406518"><a name="zh-cn_topic_0000001210206047_p1360513406518"></a><a name="zh-cn_topic_0000001210206047_p1360513406518"></a>是</p> 1088</td> 1089<td class="cellrowborder" valign="top" width="8.44%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p86051440205116"><a name="zh-cn_topic_0000001210206047_p86051440205116"></a><a name="zh-cn_topic_0000001210206047_p86051440205116"></a>否</p> 1090</td> 1091<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p860554020511"><a name="zh-cn_topic_0000001210206047_p860554020511"></a><a name="zh-cn_topic_0000001210206047_p860554020511"></a>事件创建时的时间戳(精度为毫秒)。</p> 1092</td> 1093</tr> 1094</tbody> 1095</table> 1096 1097## EventListener<a name="zh-cn_topic_0000001210206047_section53411747134914"></a> 1098 1099事件监听类。 1100 1101### \(evt: Event\): void | Promise<void\><a name="zh-cn_topic_0000001210206047_section1915072122913"></a> 1102 1103执行的回调函数。 1104 1105- 参数: 1106 1107 <a name="zh-cn_topic_0000001210206047_table9785174213215"></a> 1108 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row3786042153218"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p8786154293216"><a name="zh-cn_topic_0000001210206047_p8786154293216"></a><a name="zh-cn_topic_0000001210206047_p8786154293216"></a>参数名</p> 1109 </th> 1110 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p127865425324"><a name="zh-cn_topic_0000001210206047_p127865425324"></a><a name="zh-cn_topic_0000001210206047_p127865425324"></a>类型</p> 1111 </th> 1112 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p678610423329"><a name="zh-cn_topic_0000001210206047_p678610423329"></a><a name="zh-cn_topic_0000001210206047_p678610423329"></a>必填</p> 1113 </th> 1114 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p87861442183219"><a name="zh-cn_topic_0000001210206047_p87861442183219"></a><a name="zh-cn_topic_0000001210206047_p87861442183219"></a>说明</p> 1115 </th> 1116 </tr> 1117 </thead> 1118 <tbody><tr id="zh-cn_topic_0000001210206047_row1478624283213"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p1078654213218"><a name="zh-cn_topic_0000001210206047_p1078654213218"></a><a name="zh-cn_topic_0000001210206047_p1078654213218"></a>evt</p> 1119 </td> 1120 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p14893142103417"><a name="zh-cn_topic_0000001210206047_p14893142103417"></a><a name="zh-cn_topic_0000001210206047_p14893142103417"></a><a href="#zh-cn_topic_0000001210206047_section1674694018507">Event</a></p> 1121 </td> 1122 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p14786194293211"><a name="zh-cn_topic_0000001210206047_p14786194293211"></a><a name="zh-cn_topic_0000001210206047_p14786194293211"></a>是</p> 1123 </td> 1124 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p1178674203218"><a name="zh-cn_topic_0000001210206047_p1178674203218"></a><a name="zh-cn_topic_0000001210206047_p1178674203218"></a>回调的事件类。</p> 1125 </td> 1126 </tr> 1127 </tbody> 1128 </table> 1129 1130- 返回值 1131 1132 <a name="zh-cn_topic_0000001210206047_t3e93239d9b134b80957bcdd4acb05291"></a> 1133 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_recc81d9f995d44aa87ba9d714b756569"><th class="cellrowborder" valign="top" width="33.93%" id="mcps1.1.3.1.1"><p id="zh-cn_topic_0000001210206047_aa3137ce511d140fba6cc93513a7a91e3"><a name="zh-cn_topic_0000001210206047_aa3137ce511d140fba6cc93513a7a91e3"></a><a name="zh-cn_topic_0000001210206047_aa3137ce511d140fba6cc93513a7a91e3"></a>参数名</p> 1134 </th> 1135 <th class="cellrowborder" valign="top" width="66.07%" id="mcps1.1.3.1.2"><p id="zh-cn_topic_0000001210206047_a4ba8ead9ee7b48298d9a6ed10659f13b"><a name="zh-cn_topic_0000001210206047_a4ba8ead9ee7b48298d9a6ed10659f13b"></a><a name="zh-cn_topic_0000001210206047_a4ba8ead9ee7b48298d9a6ed10659f13b"></a>说明</p> 1136 </th> 1137 </tr> 1138 </thead> 1139 <tbody><tr id="zh-cn_topic_0000001210206047_r64430cb15b54497f88ea6330b9a7454c"><td class="cellrowborder" valign="top" width="33.93%" headers="mcps1.1.3.1.1 "><p id="zh-cn_topic_0000001210206047_p132211316155619"><a name="zh-cn_topic_0000001210206047_p132211316155619"></a><a name="zh-cn_topic_0000001210206047_p132211316155619"></a>void | Promise<void></p> 1140 </td> 1141 <td class="cellrowborder" valign="top" width="66.07%" headers="mcps1.1.3.1.2 "><p id="zh-cn_topic_0000001210206047_a1c9b8d1829ef489e9e0fd1863190d228"><a name="zh-cn_topic_0000001210206047_a1c9b8d1829ef489e9e0fd1863190d228"></a><a name="zh-cn_topic_0000001210206047_a1c9b8d1829ef489e9e0fd1863190d228"></a>无返回值或者以Promise形式返回。</p> 1142 </td> 1143 </tr> 1144 </tbody> 1145 </table> 1146 1147- 示例: 1148 1149 ``` 1150 import worker from '@ohos.worker'; 1151 const worker = new worker.Worker("workers/worker.js"); 1152 worker.addEventListener("alert", (e)=>{ 1153 console.log("alert listener callback); 1154 }) 1155 ``` 1156 1157 1158## ErrorEvent<a name="zh-cn_topic_0000001210206047_section11241623172418"></a> 1159 1160错误事件类,用于表示worker执行过程中出现异常的详细信息,ErrorEvent类继承[Event](#zh-cn_topic_0000001210206047_section1674694018507)。 1161 1162<a name="zh-cn_topic_0000001210206047_table1224152319247"></a> 1163<table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row19241723192418"><th class="cellrowborder" valign="top" width="15.870000000000001%" id="mcps1.1.6.1.1"><p id="zh-cn_topic_0000001210206047_p10241192313249"><a name="zh-cn_topic_0000001210206047_p10241192313249"></a><a name="zh-cn_topic_0000001210206047_p10241192313249"></a>名称</p> 1164</th> 1165<th class="cellrowborder" valign="top" width="10.86%" id="mcps1.1.6.1.2"><p id="zh-cn_topic_0000001210206047_p924292382417"><a name="zh-cn_topic_0000001210206047_p924292382417"></a><a name="zh-cn_topic_0000001210206047_p924292382417"></a>参数类型</p> 1166</th> 1167<th class="cellrowborder" valign="top" width="9.34%" id="mcps1.1.6.1.3"><p id="zh-cn_topic_0000001210206047_p11242202318246"><a name="zh-cn_topic_0000001210206047_p11242202318246"></a><a name="zh-cn_topic_0000001210206047_p11242202318246"></a>可读</p> 1168</th> 1169<th class="cellrowborder" valign="top" width="11.51%" id="mcps1.1.6.1.4"><p id="zh-cn_topic_0000001210206047_p1424272312240"><a name="zh-cn_topic_0000001210206047_p1424272312240"></a><a name="zh-cn_topic_0000001210206047_p1424272312240"></a>可写</p> 1170</th> 1171<th class="cellrowborder" valign="top" width="52.42%" id="mcps1.1.6.1.5"><p id="zh-cn_topic_0000001210206047_p15242152362419"><a name="zh-cn_topic_0000001210206047_p15242152362419"></a><a name="zh-cn_topic_0000001210206047_p15242152362419"></a>说明</p> 1172</th> 1173</tr> 1174</thead> 1175<tbody><tr id="zh-cn_topic_0000001210206047_row8242122316243"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p12428233244"><a name="zh-cn_topic_0000001210206047_p12428233244"></a><a name="zh-cn_topic_0000001210206047_p12428233244"></a>message</p> 1176</td> 1177<td class="cellrowborder" valign="top" width="10.86%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p3242112322418"><a name="zh-cn_topic_0000001210206047_p3242112322418"></a><a name="zh-cn_topic_0000001210206047_p3242112322418"></a>string</p> 1178</td> 1179<td class="cellrowborder" valign="top" width="9.34%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p524262312244"><a name="zh-cn_topic_0000001210206047_p524262312244"></a><a name="zh-cn_topic_0000001210206047_p524262312244"></a>是</p> 1180</td> 1181<td class="cellrowborder" valign="top" width="11.51%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p13242102362418"><a name="zh-cn_topic_0000001210206047_p13242102362418"></a><a name="zh-cn_topic_0000001210206047_p13242102362418"></a>否</p> 1182</td> 1183<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p92421123192418"><a name="zh-cn_topic_0000001210206047_p92421123192418"></a><a name="zh-cn_topic_0000001210206047_p92421123192418"></a>异常发生的错误信息。</p> 1184</td> 1185</tr> 1186<tr id="zh-cn_topic_0000001210206047_row0242223192410"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p7242142392413"><a name="zh-cn_topic_0000001210206047_p7242142392413"></a><a name="zh-cn_topic_0000001210206047_p7242142392413"></a>filename</p> 1187</td> 1188<td class="cellrowborder" valign="top" width="10.86%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p92421239248"><a name="zh-cn_topic_0000001210206047_p92421239248"></a><a name="zh-cn_topic_0000001210206047_p92421239248"></a>string</p> 1189</td> 1190<td class="cellrowborder" valign="top" width="9.34%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p424215236241"><a name="zh-cn_topic_0000001210206047_p424215236241"></a><a name="zh-cn_topic_0000001210206047_p424215236241"></a>是</p> 1191</td> 1192<td class="cellrowborder" valign="top" width="11.51%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p0242223172411"><a name="zh-cn_topic_0000001210206047_p0242223172411"></a><a name="zh-cn_topic_0000001210206047_p0242223172411"></a>否</p> 1193</td> 1194<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p524262320241"><a name="zh-cn_topic_0000001210206047_p524262320241"></a><a name="zh-cn_topic_0000001210206047_p524262320241"></a>出现异常所在的文件。</p> 1195</td> 1196</tr> 1197<tr id="zh-cn_topic_0000001210206047_row434214463257"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p5342164692515"><a name="zh-cn_topic_0000001210206047_p5342164692515"></a><a name="zh-cn_topic_0000001210206047_p5342164692515"></a>lineno</p> 1198</td> 1199<td class="cellrowborder" valign="top" width="10.86%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p13342164622515"><a name="zh-cn_topic_0000001210206047_p13342164622515"></a><a name="zh-cn_topic_0000001210206047_p13342164622515"></a>number</p> 1200</td> 1201<td class="cellrowborder" valign="top" width="9.34%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p13342194619258"><a name="zh-cn_topic_0000001210206047_p13342194619258"></a><a name="zh-cn_topic_0000001210206047_p13342194619258"></a>是</p> 1202</td> 1203<td class="cellrowborder" valign="top" width="11.51%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p1934244652517"><a name="zh-cn_topic_0000001210206047_p1934244652517"></a><a name="zh-cn_topic_0000001210206047_p1934244652517"></a>否</p> 1204</td> 1205<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p23428465259"><a name="zh-cn_topic_0000001210206047_p23428465259"></a><a name="zh-cn_topic_0000001210206047_p23428465259"></a>异常所在的行数。</p> 1206</td> 1207</tr> 1208<tr id="zh-cn_topic_0000001210206047_row888217500256"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p78821850182511"><a name="zh-cn_topic_0000001210206047_p78821850182511"></a><a name="zh-cn_topic_0000001210206047_p78821850182511"></a>colno</p> 1209</td> 1210<td class="cellrowborder" valign="top" width="10.86%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p16882155017251"><a name="zh-cn_topic_0000001210206047_p16882155017251"></a><a name="zh-cn_topic_0000001210206047_p16882155017251"></a>number</p> 1211</td> 1212<td class="cellrowborder" valign="top" width="9.34%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p188826506257"><a name="zh-cn_topic_0000001210206047_p188826506257"></a><a name="zh-cn_topic_0000001210206047_p188826506257"></a>是</p> 1213</td> 1214<td class="cellrowborder" valign="top" width="11.51%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p1088219502255"><a name="zh-cn_topic_0000001210206047_p1088219502255"></a><a name="zh-cn_topic_0000001210206047_p1088219502255"></a>否</p> 1215</td> 1216<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p1288319505259"><a name="zh-cn_topic_0000001210206047_p1288319505259"></a><a name="zh-cn_topic_0000001210206047_p1288319505259"></a>异常所在的列数。</p> 1217</td> 1218</tr> 1219<tr id="zh-cn_topic_0000001210206047_row81624652614"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p7161746182619"><a name="zh-cn_topic_0000001210206047_p7161746182619"></a><a name="zh-cn_topic_0000001210206047_p7161746182619"></a>error</p> 1220</td> 1221<td class="cellrowborder" valign="top" width="10.86%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p316846162613"><a name="zh-cn_topic_0000001210206047_p316846162613"></a><a name="zh-cn_topic_0000001210206047_p316846162613"></a>Object</p> 1222</td> 1223<td class="cellrowborder" valign="top" width="9.34%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p61764614263"><a name="zh-cn_topic_0000001210206047_p61764614263"></a><a name="zh-cn_topic_0000001210206047_p61764614263"></a>是</p> 1224</td> 1225<td class="cellrowborder" valign="top" width="11.51%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p1817104615262"><a name="zh-cn_topic_0000001210206047_p1817104615262"></a><a name="zh-cn_topic_0000001210206047_p1817104615262"></a>否</p> 1226</td> 1227<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p3175462267"><a name="zh-cn_topic_0000001210206047_p3175462267"></a><a name="zh-cn_topic_0000001210206047_p3175462267"></a>异常类型。</p> 1228</td> 1229</tr> 1230</tbody> 1231</table> 1232 1233## MessageEvent<a name="zh-cn_topic_0000001210206047_section191271455124710"></a> 1234 1235消息类,持有worker线程间传递的数据。 1236 1237<a name="zh-cn_topic_0000001210206047_table10128155524711"></a> 1238<table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row1412855554719"><th class="cellrowborder" valign="top" width="15.870000000000001%" id="mcps1.1.6.1.1"><p id="zh-cn_topic_0000001210206047_p61283559473"><a name="zh-cn_topic_0000001210206047_p61283559473"></a><a name="zh-cn_topic_0000001210206047_p61283559473"></a>名称</p> 1239</th> 1240<th class="cellrowborder" valign="top" width="10.86%" id="mcps1.1.6.1.2"><p id="zh-cn_topic_0000001210206047_p61281155134713"><a name="zh-cn_topic_0000001210206047_p61281155134713"></a><a name="zh-cn_topic_0000001210206047_p61281155134713"></a>参数类型</p> 1241</th> 1242<th class="cellrowborder" valign="top" width="9.34%" id="mcps1.1.6.1.3"><p id="zh-cn_topic_0000001210206047_p1312855520479"><a name="zh-cn_topic_0000001210206047_p1312855520479"></a><a name="zh-cn_topic_0000001210206047_p1312855520479"></a>可读</p> 1243</th> 1244<th class="cellrowborder" valign="top" width="11.51%" id="mcps1.1.6.1.4"><p id="zh-cn_topic_0000001210206047_p612855514717"><a name="zh-cn_topic_0000001210206047_p612855514717"></a><a name="zh-cn_topic_0000001210206047_p612855514717"></a>可写</p> 1245</th> 1246<th class="cellrowborder" valign="top" width="52.42%" id="mcps1.1.6.1.5"><p id="zh-cn_topic_0000001210206047_p17128755194716"><a name="zh-cn_topic_0000001210206047_p17128755194716"></a><a name="zh-cn_topic_0000001210206047_p17128755194716"></a>说明</p> 1247</th> 1248</tr> 1249</thead> 1250<tbody><tr id="zh-cn_topic_0000001210206047_row11128105544715"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p141281755204713"><a name="zh-cn_topic_0000001210206047_p141281755204713"></a><a name="zh-cn_topic_0000001210206047_p141281755204713"></a>data</p> 1251</td> 1252<td class="cellrowborder" valign="top" width="10.86%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p9128655144715"><a name="zh-cn_topic_0000001210206047_p9128655144715"></a><a name="zh-cn_topic_0000001210206047_p9128655144715"></a>T</p> 1253</td> 1254<td class="cellrowborder" valign="top" width="9.34%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p91286553472"><a name="zh-cn_topic_0000001210206047_p91286553472"></a><a name="zh-cn_topic_0000001210206047_p91286553472"></a>是</p> 1255</td> 1256<td class="cellrowborder" valign="top" width="11.51%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p111292558478"><a name="zh-cn_topic_0000001210206047_p111292558478"></a><a name="zh-cn_topic_0000001210206047_p111292558478"></a>否</p> 1257</td> 1258<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p612905514479"><a name="zh-cn_topic_0000001210206047_p612905514479"></a><a name="zh-cn_topic_0000001210206047_p612905514479"></a>线程间传递的数据。</p> 1259</td> 1260</tr> 1261</tbody> 1262</table> 1263 1264## WorkerGlobalScope<a name="zh-cn_topic_0000001210206047_section12882825611"></a> 1265 1266worker线程自身的运行环境,WorkerGlobalScope类继承[EventTarget](#zh-cn_topic_0000001210206047_section3283104561014)。 1267 1268### 属性<a name="zh-cn_topic_0000001210206047_section3626134611546"></a> 1269 1270<a name="zh-cn_topic_0000001210206047_table5668145611542"></a> 1271<table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row196681856175413"><th class="cellrowborder" valign="top" width="15.870000000000001%" id="mcps1.1.6.1.1"><p id="zh-cn_topic_0000001210206047_p466895614541"><a name="zh-cn_topic_0000001210206047_p466895614541"></a><a name="zh-cn_topic_0000001210206047_p466895614541"></a>名称</p> 1272</th> 1273<th class="cellrowborder" valign="top" width="18.18%" id="mcps1.1.6.1.2"><p id="zh-cn_topic_0000001210206047_p466975610548"><a name="zh-cn_topic_0000001210206047_p466975610548"></a><a name="zh-cn_topic_0000001210206047_p466975610548"></a>参数类型</p> 1274</th> 1275<th class="cellrowborder" valign="top" width="7.48%" id="mcps1.1.6.1.3"><p id="zh-cn_topic_0000001210206047_p176692565546"><a name="zh-cn_topic_0000001210206047_p176692565546"></a><a name="zh-cn_topic_0000001210206047_p176692565546"></a>可读</p> 1276</th> 1277<th class="cellrowborder" valign="top" width="6.05%" id="mcps1.1.6.1.4"><p id="zh-cn_topic_0000001210206047_p5669185620548"><a name="zh-cn_topic_0000001210206047_p5669185620548"></a><a name="zh-cn_topic_0000001210206047_p5669185620548"></a>可写</p> 1278</th> 1279<th class="cellrowborder" valign="top" width="52.42%" id="mcps1.1.6.1.5"><p id="zh-cn_topic_0000001210206047_p766916567549"><a name="zh-cn_topic_0000001210206047_p766916567549"></a><a name="zh-cn_topic_0000001210206047_p766916567549"></a>说明</p> 1280</th> 1281</tr> 1282</thead> 1283<tbody><tr id="zh-cn_topic_0000001210206047_row15669185613547"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p16692567543"><a name="zh-cn_topic_0000001210206047_p16692567543"></a><a name="zh-cn_topic_0000001210206047_p16692567543"></a>name</p> 1284</td> 1285<td class="cellrowborder" valign="top" width="18.18%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p12669256175418"><a name="zh-cn_topic_0000001210206047_p12669256175418"></a><a name="zh-cn_topic_0000001210206047_p12669256175418"></a>string</p> 1286</td> 1287<td class="cellrowborder" valign="top" width="7.48%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p1266912564547"><a name="zh-cn_topic_0000001210206047_p1266912564547"></a><a name="zh-cn_topic_0000001210206047_p1266912564547"></a>是</p> 1288</td> 1289<td class="cellrowborder" valign="top" width="6.05%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p166955618549"><a name="zh-cn_topic_0000001210206047_p166955618549"></a><a name="zh-cn_topic_0000001210206047_p166955618549"></a>否</p> 1290</td> 1291<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p196697562549"><a name="zh-cn_topic_0000001210206047_p196697562549"></a><a name="zh-cn_topic_0000001210206047_p196697562549"></a>worker的名字,由new Worker时指定。</p> 1292</td> 1293</tr> 1294<tr id="zh-cn_topic_0000001210206047_row1266905611549"><td class="cellrowborder" valign="top" width="15.870000000000001%" headers="mcps1.1.6.1.1 "><p id="zh-cn_topic_0000001210206047_p7669456155412"><a name="zh-cn_topic_0000001210206047_p7669456155412"></a><a name="zh-cn_topic_0000001210206047_p7669456155412"></a>self</p> 1295</td> 1296<td class="cellrowborder" valign="top" width="18.18%" headers="mcps1.1.6.1.2 "><p id="zh-cn_topic_0000001210206047_p36698567543"><a name="zh-cn_topic_0000001210206047_p36698567543"></a><a name="zh-cn_topic_0000001210206047_p36698567543"></a><a href="#zh-cn_topic_0000001210206047_section12882825611">WorkerGlobalScope</a> & typeof globalThis</p> 1297</td> 1298<td class="cellrowborder" valign="top" width="7.48%" headers="mcps1.1.6.1.3 "><p id="zh-cn_topic_0000001210206047_p15669115675415"><a name="zh-cn_topic_0000001210206047_p15669115675415"></a><a name="zh-cn_topic_0000001210206047_p15669115675415"></a>是</p> 1299</td> 1300<td class="cellrowborder" valign="top" width="6.05%" headers="mcps1.1.6.1.4 "><p id="zh-cn_topic_0000001210206047_p106701356125415"><a name="zh-cn_topic_0000001210206047_p106701356125415"></a><a name="zh-cn_topic_0000001210206047_p106701356125415"></a>否</p> 1301</td> 1302<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="zh-cn_topic_0000001210206047_p116701563546"><a name="zh-cn_topic_0000001210206047_p116701563546"></a><a name="zh-cn_topic_0000001210206047_p116701563546"></a>WorkerGlobalScope本身。</p> 1303</td> 1304</tr> 1305</tbody> 1306</table> 1307 1308### onerror<a name="zh-cn_topic_0000001210206047_section1234812184551"></a> 1309 1310onerror?: \(ev: ErrorEvent\) =\> void 1311 1312WorkerGlobalScope的onerror属性表示worker在执行过程中发生异常被调用的事件处理程序,处理程序在worker线程中执行。 1313 1314- 参数: 1315 1316 <a name="zh-cn_topic_0000001210206047_table434981814553"></a> 1317 <table><thead align="left"><tr id="zh-cn_topic_0000001210206047_row11349131825513"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="zh-cn_topic_0000001210206047_p18349131825511"><a name="zh-cn_topic_0000001210206047_p18349131825511"></a><a name="zh-cn_topic_0000001210206047_p18349131825511"></a>参数名</p> 1318 </th> 1319 <th class="cellrowborder" valign="top" width="16.24%" id="mcps1.1.5.1.2"><p id="zh-cn_topic_0000001210206047_p93491186552"><a name="zh-cn_topic_0000001210206047_p93491186552"></a><a name="zh-cn_topic_0000001210206047_p93491186552"></a>类型</p> 1320 </th> 1321 <th class="cellrowborder" valign="top" width="7.6499999999999995%" id="mcps1.1.5.1.3"><p id="zh-cn_topic_0000001210206047_p1034921855514"><a name="zh-cn_topic_0000001210206047_p1034921855514"></a><a name="zh-cn_topic_0000001210206047_p1034921855514"></a>必填</p> 1322 </th> 1323 <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="zh-cn_topic_0000001210206047_p19349201812556"><a name="zh-cn_topic_0000001210206047_p19349201812556"></a><a name="zh-cn_topic_0000001210206047_p19349201812556"></a>说明</p> 1324 </th> 1325 </tr> 1326 </thead> 1327 <tbody><tr id="zh-cn_topic_0000001210206047_row43491218115511"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="zh-cn_topic_0000001210206047_p1935041812559"><a name="zh-cn_topic_0000001210206047_p1935041812559"></a><a name="zh-cn_topic_0000001210206047_p1935041812559"></a>ev</p> 1328 </td> 1329 <td class="cellrowborder" valign="top" width="16.24%" headers="mcps1.1.5.1.2 "><p id="zh-cn_topic_0000001210206047_p1135091805513"><a name="zh-cn_topic_0000001210206047_p1135091805513"></a><a name="zh-cn_topic_0000001210206047_p1135091805513"></a><a href="#zh-cn_topic_0000001210206047_section11241623172418">ErrorEvent</a></p> 1330 </td> 1331 <td class="cellrowborder" valign="top" width="7.6499999999999995%" headers="mcps1.1.5.1.3 "><p id="zh-cn_topic_0000001210206047_p535041814557"><a name="zh-cn_topic_0000001210206047_p535041814557"></a><a name="zh-cn_topic_0000001210206047_p535041814557"></a>否</p> 1332 </td> 1333 <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="zh-cn_topic_0000001210206047_p935017188559"><a name="zh-cn_topic_0000001210206047_p935017188559"></a><a name="zh-cn_topic_0000001210206047_p935017188559"></a>异常数据。</p> 1334 </td> 1335 </tr> 1336 </tbody> 1337 </table> 1338 1339- 示例: 1340 1341 ``` 1342 main.js 1343 import worker from '@ohos.worker'; 1344 const worker = new worker.Worker("workers/worker.js"); 1345 1346 worker.js 1347 import worker from "@ohos.worker"; 1348 const parentPort = worker.parentPort; 1349 parentPort.onerror = function(e){ 1350 console.log("worker.js onerror"); 1351 } 1352 ``` 1353 1354 1355