将__proto__分配给接口以将其转换为对象 - 聪明还是危险? [英] Assign __proto__ to an interface to turn it into an object - clever or dangerous?

查看:72
本文介绍了将__proto__分配给接口以将其转换为对象 - 聪明还是危险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用postMessage()向/从Web worker复制数据时,您可以发布一个类的对象。但在远端,收到的对象只是数据(即接口,而不是类)。

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).

一个人建议在接收时为该对象分配正确的 __ proto __ 把它变成一个班级。这有效。但这是坚实的吗?安全还是要求麻烦?

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.

推荐答案

某些浏览器可能不支持修补 __ proto __ 或者,在将来,可能会停止支持它。

Some browsers may not support patching __proto__ or, in the future, may stop supporting it.

当我的代码在 WinRT HTML应用程序。

I discovered this the hard way when my code stopped working when used in WinRT HTML applications.

正确的方式(在ES5中)将是 Object.create(Constructor.prototype,obj)查看她的更多示例

The properper way (in ES5) would be to call Object.create(Constructor.prototype, obj) (see her for more examples)

这篇关于将__proto__分配给接口以将其转换为对象 - 聪明还是危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆