Daniel Janus’s blog
Posts in category: JavaScript
2048: A close look at the source
Dust has now mostly settled down on 2048. Yet, in all the deluge of variants and clones that has swept through Hacker News, little has been written about the experience of modifying the game. As I too have jumped on the 2048-modding bandwagon, it’s time to fill that gap, because, as we shall see, the code more than deserves a close look.
Meet my little friend createTree
I’ve recently been developing an iPhone application in my spare time. I’m not going to tell you what it is just yet (I will post a separate entry once I manage to get it into the App Store); for now, let me just say that I’m writing it in JavaScript and HTML5, using [PhoneGap][1] and [jQTouch][2] to give it a native touch.
A quirk with JavaScript closures
I keep running into this obstacle every now and then. Consider this example:
> q = []
[]
> for (var i = 0; i < 3; i++)
q.push(function() { console.log(i); });
> q[0]()
3
I wanted an array of three closures, each printing a different number to the console when called. Instead, each prints 3 (or, rather, whatever the value of the variable i happens to be).