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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:34:29+00:00 2026-06-16T06:34:29+00:00

I have a Javascript library that I want to use on a web browser

  • 0

I have a Javascript library that I want to use on a web browser and also on a Node.js backend. In the library, I have multiple objects with methods defined like so:

function foo() {
  this.bar = 200;
  this.someMethod = function(baz) {
    return this.bar + baz;
  };
}

var x = new foo();

And I can use it in the client or the Node.js server by doing the following:

x.someMethod(5);
   => (returns 205)

Now, when I JSON.stringify my object, it shows up without the method.

var string = JSON.stringify(x);
   => {"bar":200}

Which means I can’t unpack the JSON on my server and use the same methods.

var parsed = JSON.parse(string);
document.write(parsed.someMethod(5));
   => (doesn't do anything. the methods are gone!)

In a class based system I’d just use a copy constructor. Something that would reconstruct the object from JSON.

function foo_copy_constructor(parsed_json) {
  f = new foo();
  f.bar = parsed_json.bar;
  return f;
}

var z = foo_copy_constructor(parsed);
z.someMethod(5);
   => (returns 205 like it should)

( jsfiddle: http://jsfiddle.net/7FdDe/ )

Basically, Is there a better way than this?

Many of my objects contain instances of other objects I’ve written with their own methods, and this seems like it would get tedious to build a constructor for every object since both the client and the server use the same library and object definitions. I know that JavaScript is based on prototypes, but I don’t really understand them since I’ve just started with JavaScript and am used to Python and class-based languages.

Thanks for any and all help!

  • 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-16T06:34:30+00:00Added an answer on June 16, 2026 at 6:34 am

    JSON.stringify only stringifies the objects that have the toJSON method. So you could simply add the toJSON method to your methods. (Remember, functions are objects too.)

    function A() {
        this.method = function() { console.log(1); };
    }
    
    var c = new A();
    JSON.stringify(c);
    "{}"
    
    A.prototype.otherMethod = function() { console.log(1); };
    
    var c = new A();
    JSON.stringify(c);
    "{}"
    
    Function.prototype.toJSON = function() { return this.toString(); };
    JSON.stringify(c);
    "{"method":"function () { console.log(1); }"}"
    

    However, when parsing this back, you get the function as a string. So you have to the strings back to functions with something like this:

    var d = JSON.parse(JSON.stringify(c));
    Object.keys(d).forEach(function(k) {
    
        // If it starts with "function"
        if (/^function/.test(d[k])) {
    
            // Get the body of the function
            var f = d[k].match(/{(.*)}/).pop();
    
            // Replace the string with a real function
            d[k] = new Function(f);
        }
    });
    
    d.method();
    1
    

    However, instead of messing with javascript like this, I’d rather suggest that you use a well-tested library like now.js.

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

Sidebar

Related Questions

I want to do Least Squares Fitting in Javascript in a web browser. Currently
I have a webpage that uses JavaScript to retrieve JSON from a web service.
I am implementing a javascript library that talks to a SOAP webservice. I use
I have an external javascript library which triggers on the change of a textarea,
I have JavaScript application, where I use client-side templates (underscore.js, Backbone.js). Data for initial
I have Javascript function defined in the main index.html file. One part of this
I have Javascript that opens another window and registers a click handler for all
I have JavaScript that is doing activity periodically. When the user is not looking
I have javascript string variable with var sttr=We prefer questions that can be answered
I want to enable drag-and-drop behaviour on my Web application. I have an image

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.