Function objects are not serializable or structured-cloneable. In order to be passed from one tab to another, an object must be serializable and deserializable. Essentially this means the object's properties must be able to be converted into a transferrable format and back out into an object.
There are simple ways to work around your problem. Here is one. Capture all of the important properties of your non-serializable object in a serializable object. Then pass the serializable object. Then deserialize.
// file.js (include this on both tabs)
function MyObject() {
this.foo;
this.bar;
}
MyObject.prototype.asdf = function(){};
Let's suppose above is your object you want to transfer from one tab to another, but cannot, because it is a function object. First, create some helper functions.
// Create and return a serialized state
MyObject.prototype.serialize = function() {
// Create a simple object of only important state properties
var simpleObject = {};
simpleObject.foo = this.foo;
simpleObject.bar = this.bar;
return simpleObject;
};
// Assign property values to this object from simple object
MyObject.prototype.deserialize = function(simpleObject) {
this.foo = simpleObject.foo;
this.bar = simpleObject.bar;
};
Now, use these helpers when sending messages. I am going to use some pseudocode here.
// Send the object from one tab to others
function sendMessageHelper() {
// We don't actually send the object, we send its serial form
var myObject = new MyObject();
var transferable = myObject.serialize();
window.postMessage(transferable);
}
// Get the object from the message
function onMessageHandler(message) {
var data = message.data;
// Recreate the state of the object by deserializing
var myObject = new MyObject();
myObject.deserialize(data);
}
Finally, make sure to include the js file that defines the object in both pages (both tabs).