Assign __proto__ to an interface to turn it into an object - clever or dangerous?

允我心安 提交于 2019-12-20 05:43:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!