Things about JavaScript I wish I knew earlier
Christian Hellman wrote another excellent JavaScript article over at Smashing Magazine. This time he addresses styles and techniques of coding that is widely supported, accepted, and expected by good JavaScript developers. If you’ve been programming JavaScript for awhile, you’ll notice much of the stuff is different now than it was less than a decade ago now that JavaScript has evolved and improved based on the needs of the JavaScript community as well as the capabilities of modern-day browsers.
Personally, the ones I’ve fallen victim to most recently are shortcut notations. Having a Java background, I always wanted to create new Objects and Arrays using the “new” keyword and constructing. During the writing of Getting Started with Dojo, Peter Higgins (the technical reviewer) pounded in my head that today, objects are created like var foo = {} and arrays are created like var bar = []. Christian discusses this in the article (and a bit more in detail).
Using JSON as a data format is another one I can strongly relate to, especially with his mention of using arrays and delimited strings in the past. Today, there will not be a project that goes by where I need data brought in and I will always choose JSON if it’s available due to its native support in modern day JavaScript.
I personally am always looking for ways to make my JavaScript run faster. As developers, we so often get caught up in the moment of getting something to work and simply implement a solution in a way that just came natural and was easy, we don’t go back to see if we can make it more efficient—especially in terms of browser memory usage. There have been many times I’ve seen a simple for loop accessing data in a way that is calling another method to fetch the data, instead of caching the data once and accessing the local cache. Christian brought up another way to make your page more efficient in terms of events that I had not thought of. His example perfectly illustrates a situation in which you want to listen for the click event on a set of list items inside an unordered list. Instead of looping through each list item and adding an event handler, you can simply add an event handler to the parent of this list (the ul). From here you can determine which li was clicked. This assuming that you understand that events in the DOM roll up from the bottom when an event occurs. This means that if you click an li,if that click is not handled on the li, it will roll up to the ul and so forth.
Finally the last few points on anonymous functions with the module pattern, configuration, interacting with the back end, and browser specific code, I cannot stress enough to read and understand these the best you can. These four points that he clearly makes are utterly important in ensuring you are an efficient JavaScript developer. This will set you apart from being a good JavaScript dev, to an excellent JavaScript dev—proving you know how the language works and how to best control it and use it as it should be used.
I highly respect Christian’s articles and his advice and I recommend you head his comments where you see fit. You can read the full article on Smashing Magazine below:




