3

MDN によると、Fileオブジェクトは Web ワーカーに渡すことができ、構造化された複製アルゴリズムを使用して適切に複製されます。ここまでは順調ですね。そして、これは私がテストしたすべてのブラウザーで機能します。

// myFile comes from a form.
// webWorker is the WebWorker object.
webWorker(myFile);  // myFile appears as-is on the other side,
                    // only losing non-enumerable properties
                    // and functions, per the spec.

しかし、これはうまくいきません:

// myFile comes from a form.
// webWorker is the WebWorker object.
myFile.customIdProperty = 'some string value';
webWorker(myFile);  // myFile appears on the other side,
                    // WITHOUT the 'customIdProperty' property.

不思議なことに、これは機能します:

// myFile is a custom object now.
// webWorker is the WebWorker object.
myFile = {};
myFile.customIdProperty = 'some string value';
webWorker(myFile);  // myFile appears on the other side,
                    // WITH the 'customIdProperty' property.

これは Chrome や Firefox などの最近のバージョンで発生するので、ブラウザの問題ではないと思います。

オプション:

  1. オブジェクトのクローンを作成すると、そのプロパティが失われます。
  2. 自分が作成したオブジェクトではなく、定義済みのオブジェクト (ファイルなど) を複製すると、追加したすべてのカスタム プロパティが失われます。これは、ブラウザーが非標準オブジェクト プロパティを無視するためです。
  3. 仕様がわかりません。オブジェクトを Web ワーカーに渡すときにカスタム プロパティが複製されないことは明らかです。

ここで何か間違ったことをしていますか?前もって感謝します ;))

4

1 に答える 1