I'm working on an extension for Chrome and I'm just wondering about the usage of "for..in" in Javascript. Let me explain the issue I've found with an example:
Suppose I have a volatile object of the type {prop1: "foo", prop2: "bar", ...}
.
I want to save this object in a string in a some syntax, e.g., "prop1=foo;prop2=bar;..."
. Here is the code I used:
var fun = function(data) {
var str = "";
for (var i in data) {
str += i + "=" + data[i] + ";";
}
return str;
};
log(fun({x:1, y:2}));
That fun
returns the following string: undefinedx=1;y=2;
.
I can't really see why it follows this behavior. I know I can use JSON to "stringify" something like this, but actually I just want to understand why it happens.