It's not just your setter that's broken, but also your getter. After serializing your object to a JSON string, the result is something like
{"get_URL":"http://....","get_Extensions":["extension1","extension2"]}
If you really want to reconstruct the original object, use
function getAllWebsites() {
if (localStorage.websites === undefined){
return [];
}
var parsedJSON = JSON.parse(localStorage.websites);
// Assume that parsedJSON is an array
return parsedJSON.map(function(website) {
return trackedWebsite(website.get_URL, website.get_Extensions);
});
}
You can define a custom JSON serialization and parsing method if you need more flexibility - see JSON.stringify
and JSON.parse
.
There's another issue with your code. It's not idiomatic, because your getters/setters are prefixed with set_
and get_
. Your "trackedWebsite" object works as follows, at the moment:
var url = website.get_URL;
website.set_Extensions = ...;
var extensions = website.get_Extensions;
This should actually be
var url = website.URL; // Being at the right side implies getter
website.extensions = ...; // Assignment implies setter
var extensions = website.extensions; // Being at the right side implies getter
Or at the very least:
var url = website.get_URL();
website.set_Extensions(...);
var extensions = website.get_Extensions();