I am not getting expected values through this code, please help me to identify the bug in this code. I am newbie to java script, I doubt the problem might be due to object creation or object scope. I debugged it in several ways but unable to identify the problem.
Also please tell me if the data I am creating is in JSON format or not. If not what is the equivalent JSON format ?
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);
Instead of getting 0 and 1 as ID's and names as 'from' and 'to' I am getting following values
-1
0
to
to
Update 1 : here PS means page scope which is already declared, edited the alerts to print variables.
PS? And you arealertingstringsand not the value of a variable.PS.data.formData.push({"id":id++, "setDef":[PS.setDef[0]]});the id variable hasn't been defined before.