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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:30:29+00:00 2026-05-30T21:30:29+00:00

My final task is to fully recover object previously saved using JSON. For now

  • 0

My final task is to fully recover object previously saved using JSON. For now JSON only allows to recover data, but not behaviour. A possilbe solution is to create a new object (lets call it obj) and copy data from JSON-recovered-object to obj. But it doesn’t look nice for me. What I am asking, is there a way to dynamically change object prototype in JavaScript?

It’s how I solve problem at the moment (using self-made copy method):

(this code on JSFiddle)

function Obj() {
    this.D = "D";
    this.E = "E";
    this.F = "F";

    this.toString = function () {
        return this.D + " * " + this.E + " * " + this.F;
    };

    this.copy = function (anotherObj) {
        for (var property in anotherObj) {
            if (isDef(anotherObj[property]) && isDef(this[property])) {
                this[property] = anotherObj[property];
            }
        }
    }
}
;

$(document).ready(function () {
    var str = $.toJSON(new Obj());
    $("#result").append("<p>JSON: " + str + "</p>");
    var obj = new Obj();
    obj.copy($.parseJSON(str));
    $("#result").append("<p>Recovered obj: " + obj.toString() + "</p>");
});

function isDef(variable)
{
    return typeof variable !== undefined;
}
  • 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-30T21:30:30+00:00Added an answer on May 30, 2026 at 9:30 pm

    There are easier methods provided by many popular JS libraries.

    For example, if you are using jQuery, you might use the jQuery.extend() method instead of your copy function like so:

    var obj = $.extend(new Obj(), $.parseJSON(str));
    

    Forked jsFiddle here.

    EDIT: Based on ideas from this question, I was able to get the restored object to have all the nested functionality as well, see the updated jsFiddle.

    The core idea is to use prototypes instead of properties, and then make sure the object restored from JSON (which is just data) is the first parameter to $.extend():

    function Obj2() {
        this.A = "A";
    }
    Obj2.prototype.toString = function() {
        return this.A;
    };
    
    function Obj() {
        this.A = new Obj2();
        this.D = "D";
        this.E = "E";
        this.F = "F";
    }
    Obj.prototype.toString = function() {
        return this.A.toString() + " * " + this.D + " * " + this.E + " * " + this.F;
    };
    
    var str = $.toJSON(new Obj());
    $("#result").append("<p>JSON: " + str + "</p>");
    var obj = jQuery.extend($.parseJSON(str), new Obj());
    $("#result").append("<p>Recovered obj: " + obj.toString() + "</p>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So with the final releases of Python 3.0 (and now 3.1), a lot of
I have a AjaxEditableLabel AjaxEditableLabel<String> task = new AjaxEditableLabel<String>(task, new PropertyModel<String>(getModel(), task)); Now what
I have a custom class object in PHP named product : final class product
I am using linux cpanel shared hosting. Am using http://aaa.com/script.php to scrape data from
I came across code to stop execution's task. private final ExecutorService executor = Executors.newSingleThreadExecutor();
I am trying to perform a task that may not be possible. I haven't
im currently working on a classification task using language modelling. The first part of
Android passing multiple data to notification when i click on notification its only open
am using asynchronous task for hitting the web service url and retrieve the result
Final question for the night. And apologies for the complete noobness of this. 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.