diff --git a/zh-cn/application-dev/reference/apis/js-apis-worker.md b/zh-cn/application-dev/reference/apis/js-apis-worker.md index 14a34cc66e670faaa7a59280a4a3827722837217..5d1ffcb5c93d07f308505ff2c0de219101ddc49c 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-worker.md +++ b/zh-cn/application-dev/reference/apis/js-apis-worker.md @@ -704,6 +704,56 @@ parentPort.onerror = function(e){ | ArrayBuffer | 提供transfer能力 | 是 | | TypedArray | | 是 | +特例:传递通过自定义class创建出来的object时,不会发生序列化错误,但是自定义class的属性(如Function)无法通过序列化传递。 +```js +// main.js +import worker from '@ohos.worker'; +const workerInstance = new worker.Worker("workers/worker.js"); +workerInstance.postMessage("message from main to worker"); +workerInstance.onmessage = function(d) { + // 当worker线程传递obj2时,data即为obj2。data没有Init、SetName的方法 + let data = d.data; +} +``` +```js +// worker.js +import worker from '@ohos.worker'; +const parentPort = worker.parentPort; +class MyModel { + Init() { + this.name = "wzy" + this.age = 18 + } + SetName() { + this.name = "WZY" + } +} +parentPort.onmessage = function(d) { + console.log("worker.js onmessage"); + let data = d.data; + let func1 = function() { + console.log("post message is function"); + } + let obj1 = { + "index": 2, + "name1": "zhangshan", + setName() { + this.index = 3; + } + } + let obj2 = new MyModel(); + // parentPort.postMessage(func1); 传递func1发生序列化错误 + // parentPort.postMessage(obj1); 传递obj1发生序列化错误 + parentPort.postMessage(obj2); // 传递obj2不会发生序列化错误 +} +parentPort.onmessageerror = function(e) { + console.log("worker.js onmessageerror"); +} +parentPort.onerror = function(e) { + console.log("worker.js onerror"); +} +``` + ### 内存模型 Worker基于Actor并发模型实现。在Worker的交互流程中,JS主线程可以创建多个Worker子线程,各个Worker线程间相互隔离,并通过序列化传递对象,等到Worker线程完成计算任务,再把结果返回给主线程。