Note to self, scope is important.

Being that this was an issue just last evening, I thought I would share something that I have encountered when writing Javascript scripts.  First of all, let me state that Javascript syntax is extremely forgiving.  You can do all kinds of  unorthodox declarations of variables as well as use variables in all kinds of strange ways.  You can take a variable, store a string in it, then a number, then an object and then back again.  Weakly typed would be the phrase.  The one thing that I would like to note, as it was my big issue last evening, is scope of your variables.  So long as you are careful about defining the scope of any given variable then you are ok, if not, you could have a problem just like I did.  So, let’s start with scope and how it works.

Scope: A mouthwash.  Yep.  There you have it.  Brush and floss every day and you’ll be a better programmer.

That’s useless.

Scope: In computer programming in general, a scope is an enclosing context.

Ok, at least this one is accurate, though it doesn’t tell us a whole lot.  Really, the way that you can look at scope is “what parts of my program can access this element?”  I know that “element” is a little vague, but you can have scope that refers to objects, variables, functions, classes and a whole set of “elements.”  You see why I chose the word.  What I am concerned with here is the scope of a variable.

So, how do we reconcile this in programming terms?  Well, a variable can be a global, i.e. it can be accessed by anything.  Global variables are bad and should be eliminated with impunity.  I recommend a pistol.  Seriously, though, if you have a global variable that is storing anything that might be more than garbage text, you should probably find some other solution for your data storage needs.

Object scope.  This is much more limited.  Generally in an object you want to kind of tuck the variables away so that people don’t fool with your program.  Anything that is tucked into an object and set as private or protected will serve you well, grasshopper.  With any variable that is private or protected in an object you’ll need to write a get and a set method/function for it.  Just something that should be said

Function scope and finer grain scope.  Variables that are explicitly defined in a function are available to the entire function from the definition of the variable onward.  Variables that are defined in a for loop, though, are available only in the loop.  This being said, if you define a variable for use in a loop like so “for(var i=0; i<…” the scope is limited, however if you define a variable as such: “while(true){var i=0…” then ‘i’ will be available outside of the loop, in other parts of the function.  This is a very important distinction.

Quick thoughts on “while(true)…”  Don’t do it.  Seriously.  Bad idea.  Bad bozo, no cookie!

So, now the problem that I ran into.  Javascript will allow you to declare a variable thusly: “i=5” and it is fine with that.  If there is no prior instance of the variable ‘i’ then a global will be created.  If there is a prior variable that is accessible, it will overwrite the current value of ‘i’ as it should.  If there is a variable ‘i’ that is limited in scope and inaccessible by the current operating function, a global will be created.  This is important to know when you are doing things like writing recursive functions.

Recursion leads us directly to the problem I had last night.  I was working on a recursive function that had a loop with an incremental counter.  I just went with “for(i=0; i<other_var; i++){…”  I seemed to recall that if you declared a variable this way you would still have a nice, tight little for-loop scope to work with and you wouldn’t interfere with other functions or instances of the recursion.  I was wrong.  This kind of error is somewhat invisible.  This took a while to figure out.  The way I should have done, and ended up doing this is “for(var i=0; i<other_var; i++){…” Please note the added ‘var.’  This will fix the scope issue and my code ran without a hitch.  Fun!  So, with that in mind, kids, remember to scope your variables properly or you might find yourself in a sticky situation.  Good luck and good web work!

Similar posts in Coding, Javascript

Comments are closed.