22 February 2012
|
Block Scope in Javascript
|
|
So today I found out that Javascript has no concept of block scope. That is, the following code will work fine, because 'a' is not scoped to the block:
{ var a = 0; }{ alert(a); } However, today I worked out a pretty simple workaround that emulates block scope. Simply replace the block scope as follows: (function (){ var a = 0; }()); (function (){ alert(a); }()); This definition works fine in functions as well, so it is essentially a complete replacement for block scope. |
| Read Thoughts (1) - Write a Thought |

