2
私はプライマリのデータを保存するために、struct_one
と補助struct_two
の2つのオブジェクトを作成しています。 .push
の助けを借りてデータを追加した後。 struct_one
配列のすべてのデータに最後のデータがあります。あなたはプッシュで同じオブジェクト参照を使用し私のオブジェクトが最後のデータのみを保存する理由
var struct_one = { comments:[{comment:String}] };
var struct_two = {comment:String};
function taskElementWork() {
this. createBlockSaveNew = function() {
struct_two.comment = 1 + "RED";
struct_one.comments.push(struct_two);
console.log(struct_one.comments[1].comment); // = 1RED
struct_two.comment = 2 + "RED";
struct_one.comments.push(struct_two);
console.log(struct_one.comments[2].comment); // = 2RED
struct_two.comment = 3 + "RED";
struct_one.comments.push(struct_two);
console.log(struct_one.comments[3].comment); // = 3RED
console.log(struct_one.comments[1].comment); // = 3RED -> Why!
}
}
test = new taskElementWork();
test.createBlockSaveNew();
インデントを並べ替えることはできますか? – dwjohnston
インデックスに問題はありますか?おそらくstruct_one.comments [0] .commentで始まり、次に1に進みます。そして、2 –
また、 'this.createBlockSaveNew'の' this'はおそらくあなたが思っていることをしません。 – Andy