JavaScriptのオブジェクトを値渡しする方法ってないでですか・・・for(var soeji in obj ){ obj2[soeji] = obj[soeji];}すればいいようですが、ネストしたオブジェクトは結局参照渡しになってるので意味がありません。。
私の検索したかぎりではでてこなくて・・・
再帰でコピーすればええやない
もっとスマートな方法ってないですかね・・
参照渡しの後に該当要素を削除じゃだめなの?
???参照渡しだからもうひとつのほうも削除されてしまうのでは・・・
UnderscoreでもjQueryでもいいけど。
var a = { hoge: ’hello’ };
var b = _.extend({}, a);a.hoge = ’world’;console.log(a); // Object {hoge: "world"}console.log(b); // Object {hoge: "hello"}
みんなの回答 4 件
再帰でコピーすればええやない
参照渡しの後に該当要素を削除じゃだめなの?
UnderscoreでもjQueryでもいいけど。
var a = { hoge: ’hello’ };
var b = _.extend({}, a);
a.hoge = ’world’;
console.log(a); // Object {hoge: "world"}
console.log(b); // Object {hoge: "hello"}
関連するトピックス