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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:22:38+00:00 2026-06-03T04:22:38+00:00

I am working on an app that heavily uses JavaScript. I am attempting to

  • 0

I am working on an app that heavily uses JavaScript. I am attempting to include some object-oriented practices. In this attempt, I have created a basic class like such:

function Item() { this.init(); }
Item.prototype = {
  init: function () {
    this.data = {
      id: 0,
      name: "",
      description: ""     
    }
  },

  save: function() {
    alert("Saving...");
    $.ajax({
      url: getUrl(),
      type: "POST",
      data: JSON.stringify(this.data),
      contentType: "application/json"
    });
  }
}

I am creating Item instances in my app and then saving them to local storage like such:

Item item = new Item();
window.localStorage.setItem("itemKey", JSON.stringify(item));

On another page, or at another time, I am retriving that item from local storage like such:

var item = window.localStorage.getItem("itemKey");
item = JSON.parse(item);
item.save();

Unfortunately, the “save” function does not seem to get reached. In the console window, there is an error that says:

*save_Click
(anonymous function)
onclick*

I have a hunch that the “(anonymous function)” is the console window’s way of saying “calling item.save(), but item is an anonymous type, so I am trying to access an anonymous function”. My problem is, I’m not sure how to convert “var item” into an Item class instance again. Can someone please show me?

  • 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-03T04:22:40+00:00Added an answer on June 3, 2026 at 4:22 am

    Short answer:

    Functions cannot be serialized into JSON.

    Explanation:

    JSON is a cross-platform serialization scheme based on a subset of JS literal syntax. This being the case, it can only store certain things. Per http://www.json.org/ :

    • Objects: An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
    • Arrays: An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
    • values: A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

    Functions cannot be serialized into JSON because another non-JS platform would not be able to unserialize and use it. Consider the example in reverse. Say I had a PHP object at my server which contained properties and methods. If I serialized that object with PHP’s json_encode() and methods were included in the output, how would my JavaScript ever be able to parse and understand PHP code in the methods, let alone use those methods?

    What you are seeing in your resulting JSON is the toString() value of the function on the platform you’re using. The JSON serilizer calls toString() on anything being serialized which isn’t proper for JSON.

    I believe your solution is to stop storing instances in JSON/local storage. Rather, save pertinent data for an instance which you set back to a new instance when you need.

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

Sidebar

Related Questions

Using Visual Studio 2008 and VB.Net: I have a working web app that uses
I have a working rails app that uses imagemagick commands on my local mac
Newbie question... I have a working console app that uses a shared unit I
I'm working on an app that uses this to open the homepage of the
I'm working on an app that needs to retrieve some data from a server.
I'm working on an app that has some forms that are contained within a
I'm working on a app that requires a DB and I'm getting some problems,
I'm working on an iPhone App that relies heavily on OpenGL. Right now it
I'm working on an app that connect to a webpage to get some content.
We have a working WPF app that we are looking into running in the

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.