Writing JavaScript

On my resume I say I am a programmer and list the languages PHP, JavaScript, and Python. However, I have been doing more JavaScript recently than anything. Recently, I wrote a script that was failing in unusual ways, and while working on it I learned some new tools that others might find useful as a JavaScript programmer.

The first tool, which I have been using longer than the others is js-beautify. JavaScript does not require any particular indentation, but is much easier to read and troubleshoot if it is formatted with proper indentation. This tool does that. I use Komodo Edit to write HTML and JavaScript, and the for-pay version allows you to integrate this into the IDE automatically. In order to use it with the free version IDE I wrote a Python script to format  a .js file. This requires switching from the IDE to a console window to run the script, but other than that it works well.

The next tool, which I just recently discovered is JSHint. To use it just copy and paste your JavaScript into their web page and it will point out better ways to write more correct code.

I started using Firebug after reading the OpenLayers, Beginner’s Guide. Firebug gives you a JavaScript console and the ability to click on a web page and get information about the underlying elements. Google Chrome has most of the same functionality built in, but also has a JavaScript debugger. This allows you to set break points in your JavaScript code and the page will stop at the break points and allow you to see the values of all the variables at that point.

The last tool is not actually a tool, but a JavaScript programming technique. It is called closures. The scope of a variable defines that part of the code where the value of a  variable is available. Closures can take advantage of the scope of a variable inside of functions in order to dynamically create functions. For example if you have a function with 3 parameters and you want to create a new function from it with only one parameter and the other 2 set to specific values you can do this with closures. There are probably other tricks you can do, but I used this one to help troubleshoot one of my scripts.