« Getting Back on the Horse | Main | How I Know My Wife Still Loves Me »
Sunday
Nov162014

Closures

I'm writing this shortly after my bigfoot/werewolf expose. I'm assuming my revelations are not going to rock the scientific and para-scientific communities to their respective cores, so I should continue documenting my JavaScript odyssey and get a head start on closures.

This was a new and initially mystifying programming concept to me. In some ways it's still mystifying, but it's something that just seemed to make more sense as I thought about it, which is unusual for me. That doesn't sound good, but I mean that I usually only get something when I actually do it. Adding database content to a webpage is a good example of that. I first did that with PHP, and I copied sample code, substituting my own values where appropriate. It made no sense to me until I did it a few times.

I had a different experience with closures. When first learning about them, I found the concept incomprehensible. The basic idea is you write a function, and in that function you write another function, then you return the second function. The trick about it is that any variables you make in the first function are available in the one you return. Normally function variables disappear as soon as the function is over. In this case JS takes that environment created in the first function and tacks it on to the returned function for later reference.

For some reason that clicked in my head in a way that most other novel concepts don't. I did write some sample code to make sure I actually understood, and it worked. Stuff like that always makes me a little philosophical. Curious about how the brain works.

I discussed this a bit with mentor Adam, and he said closures don't come up a lot for him, and when they do it's usually somewhat esoteric, where he needs an original value of a variable that would otherwise be inaccessible. Neither of us could come up with an example of that, but I understand what he means.

I was a little surprised at that, because it seemed to me that closures were a great way to reuse simple variable names without having to worry that you've already used that variable name somewhere else. Admittedly, I'm approaching this from a theoretical position and he's approaching it from a functional one. It may well turn out that once I start really writing code that I don't turn to closures nearly as often as I think I will while learning about them.

I think the next step for me is going to be getting back into writing code. It's been tough lately. I think it's a combination of the weather changing and some personal family stuff. I've had a lot of trouble summoning the will or energy to do much of anything. Even writing this has been a struggle. I'm sure these feelings will pass, as they always do, I just wish I knew a way to speed things along.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.