Code and Slides: Techniques, Strategies, and Patterns for Structuring JavaScript Code

This presentation was given at the spring 2012 DevConnections conference in Las Vegas and is based on my Structuring JavaScript Code course from Pluralsight. The goal of the presentation is to show how closures combined with code patterns can be used to provide structure to JavaScript code and make it more re-useable, maintainable, and less susceptible to naming conflicts.  Topics covered include:

  • Closures
  • Using Object literals
  • Namespaces
  • The Prototype Pattern
  • The Revealing Module Pattern
  • The Revealing Prototype Pattern

View more of my presentations here.

Sample code from the presentation can be found here. Check out the full-length course on the topic at Pluralsight.com.

image

comments powered by Disqus

2 Comments

  • Firstly Dan, congratulations on your excellent Pluralsight courses, I've gone through both the "Structuring JavaScript Code" and "Building ASP.NET MVC Apps with EF Code First, HTML5, and jQuery" courses, both are fantastic. Your training style and quality of content is just great, those two courses alone are worth a full years subscription to Pluralsight.

    In your Structuring JavaScript Code course you go through the two key patterns being the "Revealing Module Pattern" and the "Revealing Prototype Pattern". You also included a section in there called "Converting Code to the Revealing Prototype Pattern", which kinda makes me think that this pattern is the safest default pattern to use if there are no glaring requirements to go one way or the other.

    But in your Building ASP.NET MVC Apps with EF Code First, HTML5, and jQuery course you go down the Revealing Module Pattern and you state how you've used this for years and it's really great.

    It's a small thing, but are there are any good reasons why one should be the default pattern over the other? Is the lower memory overheads of the Prototype pattern something worth worrying about? In your experience, does it even make a difference?

    Just interested in what your preference is between these two...

  • David: Great question that more people really need to ask. Here's my general approach:

    1. If a script is what I call a "one off" script that's used directly by a given page then I normally go with the Revealing Module Pattern. These are scripts that aren't re-useable per se - they have a very specific purpose for a given page or even multiple pages.
    2. If a script is going to be re-used in an app, could be used as a library across multiple apps, etc. then I normally consider the Revealing Prototype Pattern first since it has the benefits of avoiding duplicate functions, supporting overrides, etc. That way if new versions come out devs can still leave in their overrides without touching the library script's code.

    It's definitely not cut and dry and something that really has to be evaluated on an app by app/script by script basis. But, that's the general approach I take. I wouldn't have a problem going with the Revealing Prototype Pattern as the starting point for scripts as you mention though....nothing wrong with that at all in my opinion.

    Dan

Comments have been disabled for this content.