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 7642185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:07:32+00:00 2026-05-31T09:07:32+00:00

This question is a follow-up to my last question: JavaScript Serialization and Methods .

  • 0

This question is a follow-up to my last question: JavaScript Serialization and Methods. While related, I believe this is different which is why I started this thread. Regardless…

I have a complex tree of objects that I need to pass around between pages. Because of this, I’m attempting to serialize and deserialize my objects. One of my objects in particular has several collections of child objects. Each of those child object types has a function on it, that I’m trying to call. Unfortunately, I am not having any luck. I setup a test project in an attempt to isolate the problem and get to the bottom of it. Currently, I have my JavaScript Objects defined in a seperate file called objects.js. That file looks like this:

objects.js

function MyChild() { this.init(); }
MyChild.prototype = {
    data: {
        id: 0,
        fullName: "",
    },

    init: function () {

    },

    save: function (key) {

    },

    load: function (key) {

    },

    test: function () {
        alert("Testing Child functions");
    }
}

function MyParent() { this.init(); }
MyParent.prototype = {
    data: {
        id: "",
        children: null
    },

    init: function () {
        this.data.children = [];
    },

    save: function (key) {
        window.localStorage.setItem(key, JSON.stringify(this.data));
    },

    load: function (key) {
        var temp = window.localStorage.getItem(key);
        if (temp != null) {
            this.data = JSON.parse(temp);
            $.each(this.data.children, function (i, r) {
            });
        }
    },

    test: function () {
        alert("Testing Parent functions");
    }
}

I am creating, serializing, deserializing, and attempting to interact with these objects in an .html file. That .html file is shown here:

test.html

<div>
    <input id="button1" type="button" value="Create Object Tree" onclick="button1Click();" /><br />
    <input id="button2" type="button" value="Retrieve and Execute" onclick="button2Click();" /><br />
</div>

<script type="text/javascript">
    function button1Click() {
        var child = new MyChild();
        child.data.id = 1;

        var p = new MyParent();
        p.data.id = "A";
        p.data.children.push(child);
        p.save("A");
    }

    function button2Click() {
        var storedParent = new MyParent();
        storedParent.load("A");

        storedParent.test();                          // This works
        alert(storedParent.data.children.length);     // This displays "1" like I would expect
        alert(storedParent.data.children[0].data.id); // This does NOT work. 
        storedParent.data.children[0].test();         // This does NOT work.   
    }
</script>

I’m not sure what I’m doing wrong. Can someone please help me understand this? Can somone please help me fix my example. I have a hunch that I’m not serializing MyChild objects properly. But I don’t understand how I should be serializing / deserializing them in relation to MyParent.

Thank you!

  • 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-31T09:07:33+00:00Added an answer on May 31, 2026 at 9:07 am

    You need to store your data within each object, not within its prototype.

    Data stored in in the prototype of an object is shared between all instances and won’t be serialised by JSON.stringify, so your object data never ends up in the local storage.

    To fix, add data to this within the this.init() function:

    MyChild.prototype = {
        init: function() {
            this.data = {
                id: 0,
                fullName: ""
            };
        },
        ...
    }
    
    MyParent.prototype = {
        init: function() {
            this.data = {
                id: "",
                children: []
            }
        },
        ...
    }
    

    Working sample at http://jsfiddle.net/alnitak/fdwVB/

    Note that attaching the functions in MyChild to the retrieved data is tricky. The code below appears to work:

    load: function(key) {
        var temp = window.localStorage.getItem(key);
        if (temp != null) {
            this.data = JSON.parse(temp);
            var children = this.data.children;
            for (var i = 0; i < children.length; ++i) {
                children[i] = $.extend(new MyChild(), children[i]);
            }
        }
    },
    

    But note that it works by constructing a new “default” child for each retrieved entry, and then overwriting that child’s data with the serialised values.

    This could be problematic if the constructor has “side effects”.

    A better approach might be to allow the MyChild() constructor to take an optional complete object of initial values to use instead of the default values.

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

Sidebar

Related Questions

This question is a follow-up to my last question with a reference to COM
This is a follow on from my last question relating to plotting timestamps in
this is a follow-up question to my last one here: iOS: Initialise object at
This is sort of a follow up to my last question. Im trying to
This is sort of a follow on from my last question. I am using
This is a follow up of another question I asked last night. My problem
This question is related to BuddyPress, and a follow-up question from this question I
This is a follow up to my last question. Thank you very much for
This is a follow-up to a question I asked last year about setting up
This is follow up for my last question about converting string to float I

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.