パラメータを非表示にできるように、この方法でいくつかのゲッターとセッターを使用して次のコードを取得しました。
// trackedWebsite: does not use the new to create the object trackedWebsite.
//Creates an object with private parameters only accessible by the getter
var trackedWebsite = function(url,extensions){
return {
//This returns the URL of the website
get get_URL() {
return url;
},
get get_Extensions(){
return extensions;
},
set set_Extensions(ext){
this.extensions = ext;
}
}
}
ある時点で、web サイトと呼ばれる trackedWebsite の配列の情報を localStorage に保存したいので、JSON で解析します。
localStorage.websites=JSON.stringify(websites);
最後に、ある時点ですべての Web サイトを再度読み込みます。
function getAllWebsites(){
if (localStorage.websites === undefined){
return new Array();
}
return JSON.parse(localStorage.websites);
}
これを行うと、get 関数は正常に動作しますが、受け取った配列の trackedWebsites にセッターが含まれていないことがわかりました。使用しようとすると、次のようになります: Uncaught TypeError: Object # has no method 'set_Extensions' 。なぜこれが起こるのですか?ゲッターは正しく動作するのにセッターは動作しないのはなぜですか?
編集:セッターが呼び出されるコードを追加しました
websites[index].set_Extensions(extensions);
ありがとうございました