Posts Tagged ‘development’

Scope in Javascript

November 23, 2010  |  Development

We all have those “oh…that makes sense” moments in software development.  Just after you learn something new when you feel that you must’ve only had half a brain the day before because you just couldn’t grasp a concept.   Well I’ve been hoping for that day to come with one particular frustration: scoping in Javascript.  And it finally has.

After reading http://skilldrick.co.uk/2010/11/a-brief-introduction-to-closures/ which linked to the more concise reference http://www.mredkj.com/tutorials/reference_js_intro.html#scope, the moment of clarity came.

Apparently, my frustrations with scope in Javascript was simply a misunderstanding of the var keyword.  Before this revelation, I didn’t really think the var keyword was that important.  I thought it was a way for programmers to say, “This is a new variable that I’m creating.  Not a reference to a pre-existing one”.   Oops.