JavaScript's Associative Array / Object can't be read in functions

Posted by Matrym on Stack Overflow See other posts from Stack Overflow or by Matrym
Published on 2010-05-07T07:48:19Z Indexed on 2010/05/07 7:58 UTC
Read the original article Hit count: 208

At the very beginning of the javascript file, I have:

var lbp = {};
lbp.defaults = {
    minLength: 40
};

I can successfully alert it afterwards, with:

alert(lbp.defaults.minLength);  

But as soon as I put it inside a function, when I alert, I get "Undefined". What gives, and how do I avoid this? Is it absolutely necessary to pass this variable into each function, for example, by doing:

function(lbp) { alert(lbp.defaults.minLength); }

I would have thought that defining it first, it would attain global scope and not be required to be passed in?

Thanks in advance for enlightening me :)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about global-variables