You have a single variable value which is set to an element of obj for each element in obj. Your closure has a reference to that variable, so when value changes in the enclosing scope, it also changes in the closure. Therefore, once the closure is executed after 100 milliseconds, value has been assigned to the last item in obj. To fix this, you need to introduce a new scope by adding another anonymous function, which you pass the value you need to store temporarily:
var obj = {"a": "A", "b": "B", "c":"C"};
for( var value in obj) {
(function(value) {
setTimeout(function() {
console.log(value);
},100);
})(value);
}
or:
var obj = {"a": "A", "b": "B", "c":"C"};
for(var value in obj) {
(function() {
var value2 = value;
setTimeout(function() {
console.log(value2);
},100);
})();
}
You need the paranthesis around the function definition because if "function" is the first token on a line, javascript will treat it as a named function declaration, not a function expression.
Using a closure is the standard way to introduce a new scope in Javascript as of ECMAScript 5. However, in ECMAScript 6 (which is not yet finalized or implemented), you could use a let statement instead.
p.s. You should probably declare value as a var, unless you have done so elsewhere in your function, otherewise you will create a global variable.