worker.workerData


(工作线程中可用)指代通过主线程中传递过来的数据。 它可以是任意的JavaScript值,通过主线程构造函数中的选项对象的workerData传递。 这个数据类似Web Worker中postMessage()机制,它是拷贝传递的(所以如果是较大数据里,不建议通过此方法)。

const { Worker, isMainThread, workerData } = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename, { workerData: 'Hello, world!' });
} else {
  console.log(workerData);  // Prints 'Hello, world!'.
}

An arbitrary JavaScript value that contains a clone of the data passed to this thread’s Worker constructor.

The data is cloned as if using postMessage(), according to the HTML structured clone algorithm.

const { Worker, isMainThread, workerData } = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename, { workerData: 'Hello, world!' });
} else {
  console.log(workerData);  // Prints 'Hello, world!'.
}