I'm feeling puzzled. The issue at hand is that an object seems to lose its values within a loop based on the method of access. When accessed through variables, everything appears to be in order. However, when using static expressions identical to the variables, the correct value is not returned. But, let's delve into the code:
THE FUNCTION ITSELF:
//Defining a function with an input parameter (inputString)
function strangeBehavior(inputString){
//Initializing the outputObject which will be returned
var outputObject = {PROP1: "", PROP2: ""}
// Looping through the return-Object
for(x in outputObject){
// Splitting input at <PROP1> etc.
var res = inputString.split("<" + x.toString() + ">");
// Further splitting "splitted input" at </PROP1> etc.
var res2 = res[1].split("</" + x.toString() + ">");
// res2[0] now represents the String between <PROP1></PROP1>
outputObject.x = res2[0];
// Logging x and the value assigned to x
console.log("-LOOP-START--------------------------------------");
console.log("This works fine: ");
// This part functions properly
console.log(x.toString() + ": " + outputObject.x);
console.log("This doesn't work as expected: ");
// It should be the same but it isn't
console.log("PROP1: " + outputObject.PROP1);
console.log("PROP2: " + outputObject.PROP2);
console.log("-LOOP-END----------------------------------------");
}
}
ACCESS EXAMPLE:
strangeBehavior("<PROP1>String between prop1-tags</PROP1><PROP2>Prop2-Tag-String</PROP2>");
The output shows:
PROTOCOL: -LOOP-START--------------------------------------
PROTOCOL: This works fine:
PROTOCOL: PROP1: String between prop1-tags
PROTOCOL: This doesn't work as expected:
PROTOCOL: PROP1:
PROTOCOL: PROP2:
PROTOCOL: -LOOP-END----------------------------------------
PROTOCOL: -LOOP-START--------------------------------------
PROTOCOL: This works fine:
PROTOCOL: PROP2: Prop2-Tag-String
PROTOCOL: This doesn't work as expected:
PROTOCOL: PROP1:
PROTOCOL: PROP2:
PROTOCOL: -LOOP-END----------------------------------------
I'm quite bewildered by this situation... It all sounds so bizarre. I really hope someone can offer some assistance.
Warm regards, OL