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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:21:07+00:00 2026-06-01T02:21:07+00:00

Assuming I have a JavaScript function like this… function Object1() { this.var1; this.var2; this.var3;

  • 0

Assuming I have a JavaScript function like this…

function Object1() {
  this.var1;
  this.var2;
  this.var3;

  this.method1 = function() {
    this.var3 = this.var1 + this.var2;
  }
}

…and create an instance:

var obj1 = new Object1();
obj1.var1 = 1;
obj1.var2 = 2;
obj1.method1();

Based on obj1, I want to create a JSON string from all instance variables of function Object1. Obviously, the expected JSON string should look as follows:

{"var1" : 1, "var2" : 2, "var3" : 3}

Two Questions:

1.) Is there a more elegant way to manually implementing a to_json() method in function Object1 which creates a JSON object of all instance variables and calls JSON.stringify() on that JSON object?

2.) In this regard, is there a more elegant way to create an instance of function Object1 based on a JSON string exemplarily shown above to manually implementing a from_json() method on function Object1 which calls JSON.parse() on that JSON string, iterates over the resulting JSON object and assigns each JSON value to the respective instance variable.

  • 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-06-01T02:21:08+00:00Added an answer on June 1, 2026 at 2:21 am

    i am not sure why you want custom to_json and from_json methods but if you want to just get the details of public variables of an object with its data then you can use JSON.stringify without any problem and it will also handle nesting of the objects, like this

    function Object1() {
      this.var1;
      this.var2;
      this.var3;
      this.var4;
    
      this.method1 = function() {
        this.var3 = this.var1 + this.var2;
      }
    }
    
    var obj1 = new Object1();
    obj1.var1 = 1;
    obj1.var2 = 5;
    obj1.var4 = new Object1();
    obj1.var4.var1 = 2;
    obj1.var4.var2 = 3;
    obj1.method1();
    obj1.var4.method1();
    
    JSON.stringify(obj1);
    

    so after executing this you will get "{"var1":1,"var2":5,"var4":{"var1":2,"var2":3,"var3":5},"var3":6}"

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

Sidebar

Related Questions

I have a jQuery which is like this: <script type=text/javascript> $(document).ready(function () { $(#txtAgentName).click(function
This is a newbie question: I have a pre-existing function that I would like
Assuming you have a DOM tree with nested tags, I would like to clean
Assuming that I have function get_a_color in a Django views.py: from django.utils import simplejson
I have a number of elements with the same class like this: <html> <head><title>example</title>
I have a problem with Javascript forcing the [object DOMWindow] into an function I
I normally set up my javascript code to have a function. But due to
Assuming I have only the class name of a generic as a string in
Assuming I have an open source web server or proxy I can enhance, let's
Assuming I have three tables : TableA (key, value) TableB (key, value) TableC (key,

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.