【发布时间】:2017-10-01 08:55:07
【问题描述】:
我正在创建两个对象,struct_one 和辅助struct_two 用于主要保存数据。
在.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();
【问题讨论】:
-
你能整理一下你的缩进吗?
-
会不会是索引问题?也许从 struct_one.cmets[0].comment 开始,然后转到 1,然后是 2
-
另外
thisinthis.createBlockSaveNew可能不会像你认为的那样做。 -
如果以 struct_one.cmets[0].comment 开头,结果将是 "String() { [native code] }"
标签: javascript object push