Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:30:45+00:00 2026-05-27T22:30:45+00:00

This is a follow-up question to Javascript Serialization of Typed Objects . That solution

  • 0

This is a follow-up question to Javascript Serialization of Typed Objects. That solution works OK for objects whose type is known, but now I have an object of a type that is unknown by the code that will be performing the de-serialization. There’s a base class “Sprite” that has a number of properties that need to be serialized. Any number of derived classes (such as “Player” and “Platform” etc) may derive from class Sprite and add their own properties. I also have a “MapLayer” object that contains a collection of Sprite-derived objects. How do I de-serialize the layer and all of its sprites such that each sprite will be of the correct derived type when de-serialization is complete. Do I need to use eval(“new ” + derivedTypeName + parameterList)? Is there a better way?

More details:
The Sprite base class is hard-coded, but all the derived classes are generated code. I can make the code generator generate deserialize functions for every derived class, but how do I call them appropriately from the generic base class deserialization function? There’s only one MapLayer class, and somehow it has to potentially call the deserialize function on all the classes derived from Sprite.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:30:46+00:00Added an answer on May 27, 2026 at 10:30 pm

    In order to call the derived object’s constructor function, you’ll first need to know which constructor you want to call. You don’t give details about how you’re currently encoding that type information in your serialized payload, so let’s say you’ve got something like the following:

    var MyDerivedType = function () {...};
    MyDerivedType.prototype.__derivedTypeName = 'MyDerivedType';
    MyDerivedType.deserialize = function ( input ) {
        var obj = JSON.parse( input );
        return new MyDerivedType( obj );
    };
    

    If you’re not assigning your derived types to the global scope then you’ll need to be able to address them at the time of deserialization. Here’s an example of storing them on the Sprite constructor itself:

    Sprite.derivedTypes = Sprite.derivedTypes || {};
    Sprite.derivedTypes['MyDerivedType'] = MyDerivedType;
    

    Then you can avoid using eval and call the appropriate deserializer like this:

    Sprite.deserialize = function(input) {
    
        // json parse the data string to pull out our derived type
        var o = JSON.parse(input);
    
        // delegate to the derived type's deserialize method
        return Sprite.derivedTypes[o.__derivedTypeName].deserialize(input);
    };
    

    There is nothing built-in to JavaScript/browser js environments which provides deserialization to “classes” in the sense that you’re familiar with in .net.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up question to ASP.NET How to pass container value as javascript
Note that this is a follow-up question from my previous one: How to memorize
this question is a follow-up to javascript: how to display script errors in a
This is a follow-up to this question: Using javascript:function syntax versus jQuery selector to
This is a follow-up question, but I'll include the code from before. I have
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
This is a follow on question to How do I delete 1 file from
This is a follow-on question to the How do you use ssh in a
This is a follow up question to This Question . I like (and understand)
This is a follow up question . So, Java store's integers in two's-complements and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.