Firefox SDK port sending functions

无人久伴 提交于 2019-12-13 02:18:28

问题


When I trying to send a function through port.emit it ends up null on the other side. If I change it to a simple string it works fine. Can you not send functions throw emit?

self.port.emit("requestBackground", someFunction);


回答1:


A quick look at the documentation for port.emit() indicates:

It may be called with any number of parameters, but is most likely to be called with a name for the message and an optional payload. The payload can be any value that is serializable to JSON.

Clicking on the link about it being serializable to JSON would have told you:

However, you do have to ensure that the payload can be serialized to JSON. This means that it needs to be a string, number, boolean, null, array of JSON-serializable values, or an object whose property values are themselves JSON-serializable. This means you can't send functions, and if the object contains methods they won't be encoded.



来源:https://stackoverflow.com/questions/27552270/firefox-sdk-port-sending-functions

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