是否可以通过Firefox SDK的port.emit()发送函数? [英] Is it possible to send functions through the Firefox SDK's port.emit()?

查看:95
本文介绍了是否可以通过Firefox SDK的port.emit()发送函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过port.emit发送函数时,它在另一端最终以null结束.如果我将其更改为简单的字符串,则可以正常工作.您不能通过发射发送函数吗?

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 through emit?

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

推荐答案

快速浏览

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

可以使用任意数量的参数来调用它,但是最有可能 使用消息的名称和可选的有效负载进行调用.这 有效负载可以是可序列化为的任何值JSON .

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.

点击关于它的链接可序列化为JSON 会告诉您:

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

但是,您必须确保可以将有效负载序列化为 JSON.这意味着它必须是字符串,数字,布尔值,空值, JSON可序列化值的数组,或其属性值的对象 本身可以JSON序列化. 这意味着您无法发送 函数,并且如果对象包含方法,则不会对其进行编码.

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.

这篇关于是否可以通过Firefox SDK的port.emit()发送函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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