ネストされたオブジェクトクラスをループし、そのオブジェクトのプロパティを抽出する必要があります。しかし、オブジェクト内部からプロパティ値を変更しても、これらのオブジェクトのルートオブジェクトを保持する変数の値は変更されません。オブジェクトの内部からチェックすると、新しい値が正しく適用されます。JavaScriptはすべてのオブジェクト参照のプロパティ変更を反映します
ただし、既存のプロパティを変更する代わりに新しいプロパティを追加すると、新しいプロパティにアクセスできます。
var OO = function(o, parent){
this.parent = parent || null;
this.init(o);
return this;
};
OO.prototype = {
init: function(o){
this.obj = typeof o === 'object' ? new OO(o.name, this) : o;
this.type = typeof o === 'object' ? 'object' : 'string';
if(typeof o === 'string'){
this.change();
console.log(this.parent); // Here top-level oo object holds values called in change() function. I want the variable (oo_var) holding this top-level oo to have same property values too.
this.add();
}
},
change: function(){
this.parent.obj = 'Orange'; // Holds {} before changing
this.parent.type = 'string'; // 'object' before changing
},
add: function(){
this.parent.another_obj = 'Another';
this.parent.another_type = 'another string';
}
};
var oo_var = new OO({name: 'Apple'}); // This var doesn't refresh the obj & type property values applied in change() function. But has properties added in add() function.
Iは、各レベルの兄弟とオブジェクトネスト多くのレベルを有します。
を子オブジェクトの作成 - そしてそれらを '.change()'した後。 – Bergi
本当に何をしたいですか?そして、なぜオブジェクトの作成がその親を変えるのでしょうか? – Bergi
@Bergi、私は子供から親のプロパティを変更しています。私はそれら(console.log inside())を変更して見て、それは正常に見えます。しかし、私が外から見ると(console.logの後にvar oo_var = ...)、親はまだ初期値を持っています。 [jsfiddle](https://jsfiddle.net/7rp1qxta/)を参照してください – NestedWeb