问题
When using postMessage() to copy data to/from a web worker, you can post an object that's a class. But on the far side, the received object is just the data (ie an interface, not a class).
One person suggested assign the correct __proto__
to that object on the receive side to turn it back into a class. This works. But is this solid & safe or is it asking for trouble?
I am not asking for opinion here (a big no-no), I'm asking are there specific technical issues that might bite me in the posterior.
回答1:
Some browsers may not support patching __proto__
or, in the future, may stop supporting it.
I discovered this the hard way when my code stopped working when used in WinRT HTML applications.
The properper way (in ES5) would be to call Object.create(Constructor.prototype, obj)
(see her for more examples)
来源:https://stackoverflow.com/questions/23302812/assign-proto-to-an-interface-to-turn-it-into-an-object-clever-or-dangerous