I have an object, Foo, which has a constructor and a prototype definition:
var Foo = function Foo(bar) {
this._bar = bar;
}
Foo.prototype = {
_bar: null
getBar: function() {
return _bar;
}
}
My application has an array of Foo objects, and I am using amplify.store to store them in local storage.
// save
amplify.store("FOO_ARRAY", fooArray);
// load
fooArray = amplify.store("FOO_ARRAY") || [];
When I create a new Foo object using var foo = new Foo("bar")
, foo has a method called getBar()
as expected.
However, when I retrieve a Foo which has been saved using amplify.store, the getBar()
method is not present, and the object does not have a prototype.
Here is my question: is there a way to have amplify.store preserve the object's prototype, or is there a good way to reassign that prototype after fooArray is loaded?
From amplifyjs: http://amplifyjs.com/api/store/
value: The value to store. The value can be anything that can be serialized as JSON.
Json does not support serializing functions.
You could modify your constructor to take the de serialized data and return an instance with its prototype.