worker_threads.markAsUncloneable(object)
将对象标记为不可克隆。如果在 port.postMessage() 调用中将 object 用作 message,则会引发错误。如果 object 是原始值,则这是一个无操作。
¥Mark an object as not cloneable. If object is used as message in
a port.postMessage() call, an error is thrown. This is a no-op if object is a
primitive value.
这对 ArrayBuffer 或任何类似 Buffer 的对象没有影响。
¥This has no effect on ArrayBuffer, or any Buffer like objects.
此操作无法撤消。
¥This operation cannot be undone.
import { markAsUncloneable } from 'node:worker_threads';
const anyObject = { foo: 'bar' };
markAsUncloneable(anyObject);
const { port1 } = new MessageChannel();
try {
// This will throw an error, because anyObject is not cloneable.
port1.postMessage(anyObject);
} catch (error) {
// error.name === 'DataCloneError'
}'use strict';
const { markAsUncloneable } = require('node:worker_threads');
const anyObject = { foo: 'bar' };
markAsUncloneable(anyObject);
const { port1 } = new MessageChannel();
try {
// This will throw an error, because anyObject is not cloneable.
port1.postMessage(anyObject);
} catch (error) {
// error.name === 'DataCloneError'
}浏览器中没有与此 API 等效的 API。
¥There is no equivalent to this API in browsers.