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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:29:07+00:00 2026-05-25T11:29:07+00:00

JSON.stringify() works on literal objects, such as: var myObjectLiteral = { a : 1a,

  • 0

JSON.stringify() works on literal objects, such as:

var myObjectLiteral = {
    a : "1a",
    b : "1b",
    c : 100,
    d : {
        da : "1da",
        dc : 200
    }
};
var myObjectLiteralSerialized = JSON.stringify(myObjectLiteral); 

myObjectLiteralSerialized is assigned,
“{“a”:”1a”,”b”:”1b”,”c”:100,”d”:{“da”:”1da”,”dc”:200}}”
as expected.

But, if I define the class with a ctor like this,

    function MyClass() {
    var a = "1a";
    var b = "1b";
    var c = 100;
    var d = {
        da : "1da",
        dc : 200
    };
};


var myObject = new MyClass;
var myObjectSerialized = JSON.stringify(myObject);

then myObjectSerialized is set to the empty string, “”.

I think the reason is because the class version ends up being the prototype of the instantiated class which makes it’s properties “owned” by the prototype and JSON will only stringify props owned by the instance object, myObject.

Is there a simple way to get my classes into JSON strings w/o writing a bunch of custom code?

  • 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-25T11:29:08+00:00Added an answer on May 25, 2026 at 11:29 am

    Your MyClass isn’t setting any properties on the object being constructed. It’s just creating local variables to the constructor.

    To create properties, set properties on this in the constructor, since this references the new object:

    function MyClass() {
        this.a = "1a";
        this.b = "1b";
        this.c = 100;
        this.d = {
            da : "1da",
            dc : 200
        };
    }
    

    Also, you typically wouldn’t add properties to the .prototype object inside the constructor. They only need to be added once, and will be shared among objects created from the constructor.

    function MyClass() {
        this.a = "1a";
        this.b = "1b";
        this.c = 100;
        this.d = {
            da : "1da",
            dc : 200
        };
    }
    
    MyClass.prototype.toJSON = function() {
        return; // ???
    }
    MyClass.prototype.equals = function(other) {
        if(other != null && other.prototype == this) {
            if(this.a == other.a
                && this.b == other.b
                && this.c == other.c
                && this.d.da == other.d.da
                && this.d.dc == other.d.dc)
                return true;
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sending objects to ASP.NET from JSON works fine if you have a class built
I downloaded the JSON2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js and it does not have implementation for JSON2.stringify()
JSON stands for JavaScript Object Notation. But how come languages like php, java, c
I've got some JSON data, but it's all on one line. Does anyone know
I just learned about jquery's .makeArray and I am trying to use JSON.stringify to
Im using nestedSortable and for some time it works fine but after about 1min
Is there a reliable way to JSON.stringify a JavaScript object that guarantees that the
json-min.js breaks some libraries such as jQuery mobile. The work around is to replace
I'm testing a mobile website on windows phone 7 and the JSON.stringify function is
jQuery UI Autocomplete (code below) works just fine when the returned JSON is an

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.