I have a module, which exports an array holding objects including function parameters:
var myArray = [
{
id: "firstObject",
func: function() {
console.log("Hi from function!");
}
}
];
exports myArray = myArray;
This module is included in another script, which emits an event to a client side script and passes the array as parameter:
var {myArray} = require("./array.js");
for (var i = 0; i < myArray.length; i++) {
self.port.emit("someEvent", myArray[i]);
}
On the client side I can now access the id
of the object but not the function held by the func
parameter. Actually, the func
parameter is not set at all on the client side.
Why is that? And how to circumvent that?
The docs about port.emit say:
The payload can be any value that is serializable to JSON.
and functions are not JSON serializable, and as such they won't be encoded when you emit the message.
In order to circumvent that issue, you must move that logic from your object to addon's main script or to the content script: it depends on what you need to do with that message.