Node を配列に追加してから変更するこのコードがあります。firstNode
オブジェクトはinit
関数に設定されています。
var myClass = function() {
this.items = []
this.firstNodeValues = {};
this.init = function() {
var firstIndex = false;
for (i = 10; i <= 15; i++) {
Node = {};
Node.index = i;
Node.name = "undefined";
if (i == 10) {
Node.name = "John"
this.firstNodeValues = Node;
}
this.items[i] = Node;
}
}
this.iterate = function() {
var newIndex = 10;
for (i = 10; i <= 15; i++) {
this.items[i].index = newIndex;
if (i == 10) {
this.items[i].name = "Michael";
}
newIndex++;
}
}
}
var test = new myClass();
test.init();
console.debug(test.firstNodeValues.name) // this gives value "John"
test.iterate();
console.debug(test.firstNodeValues.name) // this gives value "Michael"
2 番目のデバッグが返されます"Michael"
が、なぜですか? ノードfirstNodeValues
へのポインタがあるようitems[10]
です。後で変更ノードの値を設定した場合、それは変更ではなく、最初に設定したときとまったく同じままで
あることを望みます。FirstNodeValues
firstTime
firstNodeValues