Javascript postMessage loses Class Instance Properties

Started by Heretic86, April 27, 2021, 07:42:29 pm

Previous topic - Next topic

Heretic86

Hopefully this is an "easy" one.

I have a page that creates an Instance of a Class.  Then the Class Instance is sent to another page using page.postMessage();  When I receive the data on the other page, and I check the received data to see if the object is an Instance of the Class, it always fails, presumably because I am missing a step.

Properties Page:
class MyClass {
  constructor(name, value){
    this.name = name;
    this.value = value;
  }
}
const foo = new MyClass("bob", 123);
window.postMessage( { myMsg : foo });
function messageListener(msg){
  console.log(msg.data.myMsg instanceof MyClass); // Always returns false
}

I left a lot of code out of the examples but basically using Modules so the same class constructor is available on both pages.

I would like to be able to check the data on the receiving page to see if the object is an instance of the class.

What am I doing wrong?  Most importantly, how can I check on the receiving page to see if the received data is an instance of the class?
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

Blizzard

Try printing just:

console.log(msg.data);

to see if you actually have the proper data/object inside.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.