javascript - ReferenceError: variable is not defined -


i met issue still don't know causes it.

i have script in page:

$(function(){     var value = "10"; }); 

but browser says "referenceerror: value not defined". if go browser console , input either

10 

or

var value = "10"; 

either of them can return 10. problem script?

edit:just rid of "var" can solve problem.

it's declared inside closure, means can accessed there. if want variable accessible globally, can remove var:

$(function(){     value = "10"; }); value; // "10" 

this equivalent writing window.value = "10";.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -