Object inside of array -- works in one scope but not in another?
        Posted  
        
            by Earlz
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Earlz
        
        
        
        Published on 2010-06-10T01:13:14Z
        Indexed on 
            2010/06/10
            1:22 UTC
        
        
        Read the original article
        Hit count: 257
        
Ok I've been learning some of the more advanced aspects of Javascript and now trying to use this I'm stuck.
Here is my code:
function Data(){}
function init(state){
  var item;
  item=new Data();
  item.fieldrid=17;
  item.description='foo';
  state.push(item);
};
function findInState(state,fieldrid) {
    for (var item in state) {
        alert(item.fieldrid); //prints undefined
        if (item.fieldrid == fieldrid) {
            return item;
        }
    }
    return null;
}
var s=[];
init(s);
alert(s[0].fieldrid); //prints 17 (expected)
alert(findInState(s,17).fieldrid); //exception here. function returns null.
A running example is here at jsbin
Why does this not work? I would expect the alert in findInState to yield 17 but instead it yields undefined. 
What am I doing wrong?
© Stack Overflow or respective owner