title: How to pass class instances with function through worker_thread.Worker to use function?
body: <p>To contextualize, I would like to use class instances functions through a Worker Thread from NodeJS &quot;worker_thread&quot; module.</p>
  <p>In one <code>main.js</code> file I declare a class and instanciate a new Worker passing the new instance through the <code>workerData</code> option.</p>
  <p><strong>main.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const { Worker } = require('worker_threads');
  
  class obj {
      constructor() {
          this.a = &quot;12&quot;;
          this.b = 42;
      }
  
      c() {
          return 'hello world';
      }
  }
  
  let newobj = new obj();
  console.log({
      a: newobj.a,
      b: newobj.b,
      c: newobj.c()
  });
  //Output: { a: '12', b: 42, c: 'hello world' }
  
  let worker = new Worker('./process.js', { workerData: { obj: newobj } });
  worker.once('message', res =&gt; { console.log({ res }) });
  </code></pre>
  <p>As you may see, the worker called the <code>process.js</code> script.
  Let see it.</p>
  <p><strong>process.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const { parentPort, workerData } = require('worker_threads');
  
  let { obj } = workerData;
  
  console.log({
      a: obj.a,
      b: obj.b,
      c: obj.c()
  });
  
  parentPort.postMessage('DONE');
  </code></pre>
  <p>As you know, this code throw an error: <code>TypeError: obj.c is not a function</code>. In fact, after checking Worker Thread documentation (<a href="https://nodejs.org/dist./v10.22.0/docs/api/worker_threads.html#worker_threads_new_worker_filename_options" rel="noreferrer">https://nodejs.org/dist./v10.22.0/docs/api/worker_threads.html#worker_threads_new_worker_filename_options</a>) I discover that Worker could not be an Object with function. In reality, it works but all functions are not cloned.
  <a href="https://i.stack.imgur.com/uh0TY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/uh0TY.png" alt="Screen capture of worker_thread workerData options definition in NodeJS official documentation" /></a></p>
  <p>I am really confused because I do not know how to solve this problem. In fact, this exemple is easy because of simplify a complex situation but in my case I absolutly need to call the c function in <code>process.js</code> side but I do not know how to do it differently.</p>
  <p>I hope you may help me. Thanks in advance for your time.</p>