javascript - What does `var` do in the global scope? -


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

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -