Jump to content

Javascript postMessage and Class instanceOf

Recommended Posts

Im messing around with Classes, Inheritance, and some other stuff but to keep it "simple" I'll just say ES6 Classes

On one page, I create an instance of a class

class MyClass {
  constructor(name = "", value = ""){
    this.name = name;
    this.value = value;

const foo = new MyClass("Tom", "123");

There is a lot of other stuff going on, so in order to visually organize the data for the user (there is a LOT), I use a separate page to modify one aspect of the data that they can change.  The sub page then uses postMessage to send an instance of the class that is created on the subpage back to the main page, but it loses its Class Type.

let msg = { pageMsg : { myData: foo } };

console.log(msg.data.myMsg instanceof MyClass); // This always returns true on this page

Then I process the message back on the main page:

// Main Page
function messageListener(msg){
  console.log(msg.data.myMsg instanceof MyClass); // This always returns false

I left out all the security and module stuff to keep it easy.

Is there a way to send a message object from one page to another and retain the Class Instance?


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...