Week 1: What I Learned in JavaScript This Week

Well it’s been a while since I posted. I have backlog of Project 365 pictures to post this weekend. I’m just too lazy to post them every day. ūüôā

As I wrote here, during the month of May I’m challenging myself to learn something new about JavaScript each week, something I didn’t understand previously. I’ll post a brief summary of what I learned on Fridays…starting today. ūüôā

Also warning, this is in no way meant to be a JS tutorial. These posts are nothing more than field notes. I also have NOT mastered the concepts I’m learning…obviously not, since it’s only been a week. It’s very well possible that something I post here could be incorrect. If I find that any of this stuff is¬†incorrect later, I’ll edit accordingly.

So, this week I was curious about the usage of “self” I see sometimes when working with JS. I never understood what it meant.¬†I Googled it, and ended up finding an explanation here.¬†In a nutshell, when people use¬†self¬†, they’re storing the current object into a variable. So the word “self” isn’t actually a part of JS, it’s just a naming convention. Technically you could use any word (as long as it’s not a reserved word). But “self” makes sense if the purpose is to refer back to the current object.

Well, that article I linked to on self led to the concept of “closures”. I’ve been introduced to closures before, but I¬†decided to read up on them since I’d gotten rusty. This page gave a nice explanation. Closures are functions within functions…but the inner function (aka the closure) has access to all of the outer function’s variables and parameters. The tricky part was wrapping my head around the fact that closures still have access to the variables/parameters of the outer function, even after the outer function has already returned a value. I set up a JS Fiddle to play around with one of the examples. Literally had to stare at it¬†and manipulate it a little each day before the concept finally made sense. I didn’t get through the rest of the closure examples here this week, so I’ll continue with that this coming week.

So what’s the practical point of closures? I’m still trying to wrap my head around that, but from what I’ve learned so far, they seem to be a way to keep your code more concise and organized, and they can reduce the need for global variables. Maybe it makes sense to create closures in situations where you have a group of related functions for a specific purpose? This page gives some more examples.

Closures can be used to recreate private/public functions in JavaScript (something that other languages can do). The WordPress Codex page on JS Best Practices links to a  nice write-up on that.

Many of the WordPress themes I work with contain jQuery, and closures are everywhere. Now I’ll be aware of them, and will spend extra time figuring out what they’re doing in relation to their outer functions. Doing this will, I hope, help me understand closures better and the practical situations where they’re used.

Thanks for joining me as I set off on the journey to learn more about JS than I ever did before.

Leave a Reply