JavaScript: What are Closures?

So maybe you’ve heard of JavaScript closures before but you’re wondering, what exactly are they? Well, when it comes to JavaScript, you can have variables that belong to a local (a variable declared within a specific function) or global scope (variables that are accessible from anywhere in the code). You can make variables private by using closures.

Take a look at the following example:

var countUp = (function () {
var count = 0;
return function () {return count += 1;}
})();

countUp();

Now, let’s break it down:

  • The variable countUp is assigned the value of a self-invoking function.
  • It sets the count to 0 and returns a value that adds 1 to the current count.
  • It has access to the count in the parent scope.
  • The variable count is protected by the scope of the anonymous function, and can only be changed using the countUp function.

WDI Day Thirteen: Project Presentations & Object-Oriented Programming

In the morning we presented our projects in a science-fair style, where several of us had our laptops set up on boxes. I realized I was way more excited to talk to people about the logic behind my incomplete Hangman project than I was about the logic behind my Flash Cards. It just goes to show that it’s better to work on something that you can get excited about because you’ll work even more at getting it right.

After projects, it was a day focussed on object-oriented programming with constructor functions and prototypes. I’m realizing I’m understanding the concepts a lot better now, but certain concepts are still a bit of a struggle, like when to use return statements.

Nevertheless, I’m excited to see how things pan out with this program. Next week we start working with the back-end, so that shall be interesting.

hangman

Resources

WDI Day Six: DOM, Events, and Callbacks

I haven’t felt like I’ve been the strongest when it comes to JavaScript, so I was really excited to dive into more code today. We reviewed a few concepts I was familiar with, like working with the DOM (or Document Object Model), events, and callback functions.

Although the lecture didn’t consist of too much new information for me, the hands-on assignments were what really helped. Having to struggle to figure out how to create different functionalities proved to be really useful. One task I tackled included creating a JavaScript stop watch. Sometimes the struggle of getting something to work can be a real headache, but at the same time, once it’s actually functioning properly, it can give you a real sense of relief and accomplishment.

Helpful Resources

WDI Day Five: PseudoCoding and Intro to JS

Finally, the day I was waiting for arrived – working with JavaScript! Although today was also more of a review for me, I did find myself realizing I definitely need to do some extra work in my free time this weekend just to make sure I’m at the level I want to be at with JavaScript.

In the morning we did a few exercises involving PseudoCoding, which I realized is a pretty difficult concept to wrap my mind around. I’ll have to make sure I practice writing pseudo code and will check with my instructors to make sure I’m doing that properly.

We also got to play around with a fun and addictive game, Cookie Clicker:

cookie-clicker

Once we dug into JavaScript, we reviewed data types, conditionals, and ended the day with objects and functions. One of our homework assignments definitely looks like it will be a lot of fun to fiddle around with – it’s a choose your own adventure where the prompt boxes will change based on user input. I’m excited to get started, and hope I don’t run into too many issues. Getting started with JavaScript for any assignment is usually a little tough for me. I’m hoping that will change fairly soon.

Some Fun Resources