我没有通过此代码获得预期值,请帮我识别此代码中的错误。我是java脚本的新手,我怀疑问题可能是由于对象创建或对象范围。我通过多种方式对其进行了调试,但无法确定问题。
另请告诉我,我创建的数据是否是JSON格式。如果不是等效的JSON格式是什么?
PS.id = -1;
PS.data = {
formData:[]
};
PS.setDef = [];
PS.item = {};
if(cond){
PS.item.Name = "From";
PS.item.Type = "STRING";
PS.setDef.push(PS.item);
alert(PS.setDef[0].Name);
PS.item.Name = "To";
PS.item.Type = "STRING";
PS.setDef.push(PS.item);
alert(PS.setDef[1].Name);
}//here alerts are coming properly. getting 'from' and 'to' in alerts
PS.data.formData.push({"id":id++, "setDef":[PS.setDef[0]]});
PS.data.formData.push({"id":id++, "setDef":[PS.setDef[1]]});
//expected id 0
alert(PS.data.formData[0].id);
//expected name 'from'
alert(PS.data.formData[1].setDef[0].Name);
//expected id 1
alert(PS.data.formData[1].id);
//expected name 'to'
alert(PS.data.formData[1].setDef[1].Name);
不是将0和1作为ID而名称为'from'和'to',而是获得以下值
-1
0
to
to
更新1:此处PS表示已声明的页面范围,编辑警报以打印变量。
答案 0 :(得分:1)
您的代码有几个逻辑错误和拼写错误。这是更正后的版本(已在**HERE**
)
PS.id = -1;
PS.data = {
formData:[]
};
PS.setDef = [];
if(cond){
// *** HERE ***
// Your code was creating just one object instance in
// PS.item, pushing it, mutating and pushing it again
// The "alert" call was apparently correct just because
// it was done before mutating the object. Just moving
// both alerts at the end would have shown the problem.
PS.setDef.push({Name: "From", Type: "STRING"});
alert(PS.setDef[0].Name);
PS.setDef.push({Name: "To", Type: "STRING"});
alert(PS.setDef[1].Name);
}
// **HERE**
// id++ increments a value but returns the value BEFORE the increment.
// Thus formData[0] is expected to have .id == -1, not 0.
// If you want the value AFTER the increment then use ++id.
PS.data.formData.push({"id":++id, "setDef":[PS.setDef[0]]});
PS.data.formData.push({"id":++id, "setDef":[PS.setDef[1]]});
//expected id 0
alert(PS.data.formData[0].id);
//expected name 'from'
// *** HERE ***
// In the original code there is a typo (formData[1] instead of formData[0])
alert(PS.data.formData[0].setDef[0].Name);
//expected id 1
alert(PS.data.formData[1].id);
//expected name 'to'
alert(PS.data.formData[1].setDef[1].Name);
答案 1 :(得分:0)
这里有两件事。 首先是id,将代码更改为以下内容,
PS.data.formData.push({"id":++PS.id, "setDef":[PS.setDef[0]]});
PS.data.formData.push({"id":++PS.id, "setDef":[PS.setDef[1]]});
因为当你将id ++分配给id时你的id以-1开头,所以它首先分配当前id的值,然后增加id的值,因此得到-1和0。
其次,当您按下PS.item时,实际上是将引用传递给对象而不是实际对象。因此,当您更新PS.item的值时,它会反映在两个地方。尝试创建一个新对象并推送它,而不是覆盖旧对象。
在分配值之前,使用PS.item = {};
创建新对象。这应该有用。
答案 2 :(得分:0)
您刚刚在代码的这一部分中引用了两次PS.item:
PS.item.Name = "From";
PS.item.Type = "STRING";
PS.setDef.push(PS.item);
alert(PS.setDef[0].Name);
//changing the value of PS.item
PS.item.Name = "To";
PS.item.Type = "STRING";
PS.setDef.push(PS.item);
alert(PS.setDef[1].Name);
您可以通过这种方式编写代码,并对结果感到满意:
//creating first instance
PS.item = {};
PS.item.Name = "From";
PS.item.Type = "STRING";
PS.setDef.push(PS.item);
alert(PS.setDef[0].Name);
//creating the second instance
PS.item = {};
PS.item.Name = "To";
PS.item.Type = "STRING";
PS.setDef.push(PS.item);
alert(PS.setDef[1].Name);
有了主意吗?