You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否将ES6类传递给Workerize工具?

Can You Pass an ES6 Class to Workerize?

Short answer: No, you can't directly pass an ES6 class to Workerize and call instance methods like worker.getClassInfo() as you've shown. Here's why, plus how to make it work:

Why Your Approach Won't Work

Workerize works by packaging functions (or exported functions from a module) into a separate Worker script, then proxying method calls via asynchronous message passing between the main thread and the Worker.

Your code runs into two critical issues:

  1. ES6 classes need to be instantiated with new before you can call instance methods like getClassInfo(). When you pass SampleClass directly to Workerize, the Worker doesn't create an instance of the class—so there's no instance context for the method to run against.
  2. Even if you tried to instantiate the class in the main thread first, the class instance (including private properties like classInfo) can't be serialized and sent to the Worker. Worker communication relies on structured cloning, which doesn't preserve class instance context or private properties.

Working Solutions

1. Export a Factory Function from Your Worker Module

The most reliable approach is to export a function that creates an instance of your class inside the Worker and returns an object with the methods you want to expose. This keeps the class instance and its state contained within the Worker.

Update worker.ts:

export interface SampleInterface {
  getClassInfo(): string;
}

export class SampleClass implements SampleInterface {
  private classInfo: string;

  constructor(initialInfo: string) {
    this.classInfo = initialInfo;
  }

  public getClassInfo() {
    return this.classInfo;
  }
}

// Factory function to create and expose the class instance
export function createSample(initialInfo: string) {
  const instance = new SampleClass(initialInfo);
  return {
    getClassInfo: () => instance.getClassInfo()
  };
}

Then in index.ts:

const worker = workerize('./worker.ts');

// Create the instance inside the Worker (returns a proxy)
const sampleInstance = await worker.createSample('Hello from Worker!');
// Call the method asynchronously
const info = await sampleInstance.getClassInfo();
console.log(info); // Logs "Hello from Worker!"

2. Use Static Methods (For Stateless Scenarios)

If your class doesn't need instance-specific state, you can use static methods and export them directly. This works because static methods don't rely on an instance context.

Update worker.ts:

export interface SampleInterface {
  getClassInfo(): string;
}

export class SampleClass implements SampleInterface {
  private static classInfo: string;

  public static setClassInfo(info: string) {
    this.classInfo = info;
  }

  public static getClassInfo() {
    return this.classInfo;
  }
}

// Export static methods directly
export const setClassInfo = SampleClass.setClassInfo;
export const getClassInfo = SampleClass.getClassInfo;

Then in index.ts:

const worker = workerize('./worker.ts');

await worker.setClassInfo('Static info from Worker!');
const info = await worker.getClassInfo();
console.log(info); // Logs "Static info from Worker!"

Key Note

All methods returned by Workerize are asynchronous—even if your original method is synchronous. You must use await or .then() when calling them, since cross-Worker communication is always async.

内容的提问来源于stack exchange,提问作者jbk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:21:43