javascript - What does `var` do in the global scope? -
this question has answer here:
in html <script> tag, outside of function, var have meaning?
is there difference between these 3 notations?
<script> var = 1; b = 1; window.c = 1; </script>
at top level (outside functions) var declares global variable.
to avert this, wrap code in function.
(function () { var = 1; }()); 'a' in window; // => false despite each form declares global variable, have subtle differences. var creates undeletable property on window, , subject typical hoisting rules whereby property exist on window @ start of var statement's execution context. other forms create deletable property on window @ time line of code executed.
see this answer more info.
Comments
Post a Comment