次のコードがあります (jQquery ライブラリを使用しています)。
var obj = {};
var objstring = '{"one":"one","two":"two","three":"three"}'
// first console output
console.log(objstring);
var jsonobj = $.parseJSON(objstring);
// second console output
console.log(jsonobj);
obj.key = jsonobj;
obj.key.test = "why does this affect jsonobj? (even in the second console output)";
// third console output
console.log(jsonobj);
私の質問: obj.key = jsonobj を実行すると、新しい obj.key の値が変更されます。jsonobj の値も変更されるのはなぜですか? そして、どうすればそれを回避できますか?(jsonobj の新しい「コピー」が必要です)。
このテスト ケースを作成しました: http://jsfiddle.net/WSgVz/