Adding methods to JSON read object
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2838219/easiest-way-to-convert-json-data-into-objects-with-methods-attached">Easiest way to convert json data into objects with methods attached?</a><br>
  <a href="http://stackoverflow.com/questions/11810028/casting-plain-objects-to-function-instances-classes-in-javascript">Casting plain objects to function instances (“classes”) in javascript</a>  </p>
</blockquote>



<p>I have an object as follows:</p>

<pre><code>var Person = function(name, age) {
  this.name = name,
  this.age = age;
}

Person.prototype.talk = function(message) {
    console.log(message);
}
</code></pre>

<p>I create objects as follows:</p>

<pre><code>var person = new Person("Test", 20);
</code></pre>

<p>These objects are then saved as JSON (stringify) in browser's local storage</p>

<p>When I read the object back, I get the data, but I don't get the methods attached, i.e. talk() is not available as a method any more. How do I attach them again?</p>

<pre><code>window.localStorage["person"] = JSON.stringify(new Person('Test', 20));
var person = window.localStorage["person"];
person.talk("Hello");
</code></pre>

<p>The error I get is </p>

<pre><code>Uncaught TypeError: Object {"name":"Test","age":20} has no method 'talk' 
</code></pre>

<p>Obvious, right? But how do I tell that this is a Person object? Or should I just copy the attributes from the read object onto a new Person object, is that the only way?</p>
