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

  • SEARCH
  • Home
  • 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 6074521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:23:29+00:00 2026-05-23T10:23:29+00:00

I have many JavaScript objects in my application, something like: function Person(age) { this.age

  • 0

I have many JavaScript objects in my application, something like:

function Person(age) {
    this.age = age;
    this.isOld = function (){
        return this.age > 60;
    }
}
// before serialize, ok
var p1 = new Person(77);
alert("Is old: " + p1.isOld());

// after, got error Object #<Object> has no method 'isOld'
var serialize = JSON.stringify(p1);
var _p1 = JSON.parse(serialize);
alert("Is old: " + _p1.isOld());

See in JS Fiddle.

My question is: is there a best practice/pattern/tip to recover my object in same type it was before serialization (instances of class Person, in this case)?

Requirements that I have:

  • Optimize disk usage: I have a big tree of objects in memory. So, I don’t want to store functions.
  • Solution can use jQuery and another library to serialize/unserialize.
  • 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-23T10:23:30+00:00Added an answer on May 23, 2026 at 10:23 am

    JSON has no functions as data types. You can only serialize strings, numbers, objects, arrays, and booleans (and null)

    You could create your own toJson method, only passing the data that really has to be serialized:

    Person.prototype.toJson = function() {
        return JSON.stringify({age: this.age});
    };
    

    Similar for deserializing:

    Person.fromJson = function(json) {
        var data = JSON.parse(json); // Parsing the json string.
        return new Person(data.age);
    };
    

    The usage would be:

    var serialize = p1.toJson();
    var _p1 = Person.fromJson(serialize);
    alert("Is old: " + _p1.isOld());
    

    To reduce the amount of work, you could consider to store all the data that needs to be serialized in a special “data” property for each Person instance. For example:

    function Person(age) {
        this.data = {
            age: age
        };
        this.isOld = function (){
            return this.data.age > 60 ? true : false;
        }
    }
    

    then serializing and deserializing is merely calling JSON.stringify(this.data) and setting the data of an instance would be instance.data = JSON.parse(json).

    This would keep the toJson and fromJson methods simple but you’d have to adjust your other functions.


    Side note:

    You should add the isOld method to the prototype of the function:

    Person.prototype.isOld = function() {}
    

    Otherwise, every instance has it’s own instance of that function which also increases memory.

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

Sidebar

Related Questions

DevExpress components have many client side objects like ASPxClientComboBox, ASPxClientButton etc. which can be
There have been many JVM languages in the recent few years including Javascript, Python,
I've seen (and used) code to have a link spawn a javascript action many
I have a textarea with many lines of input, and a JavaScript event fires
Wondering how to open many new windows with Javascript. I have found plenty of
How is your javaScript code organized? Does it follow patterns like MVC, or something
I have just created a mid-sized web-application using Java, a custom MVC framework, javascript.
I'm pretty sure that many people have thought of this, but for some reason
I have found this example on StackOverflow: var people = new List<Person> { new
I have a HTML page where there are many <a onclick=javascript> What i need

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.