Thinking in JavaScript – A-ha!

I had the great honor of presenting ‘Thinking in JavaScript’ at the Java Users’ Group last night. I love leading a discussion of learning, and this was a great example of it. I was even asked during conversation afterwards if I was a member of ‘the club’ [the Java Users’ Group – JUG]. Well, do I have to know Java to be part of the JUG? I guess not. What a fun and welcoming crowd.

Our premise was thus: you know object-oriented development. You know C-style syntax from either C, C++, Java, C#, or any of the derivatives. You’re approaching JavaScript, and need to get into the mindset of how JavaScript works.

The majority of JavaScript is very much identical to all C-style languages: curly braces, semi-colons, objects, inheritance, debuggers, etc. But JavaScript is also different in a few ways we need to wrap our heads around. During the discussion, we identified 5 points that make JavaScript unique:

  • A JavaScript object is like a dictionary

    • Roughly like Dictionary<string, object>
  • A JavaScript method is an object

    • You can assign and retrieve them
  • All function parameters are optional

    • No function overloading, but no need
  • Constructors declare instance level variables

    • They aren’t private
  • All JavaScript objects reference a prototype

    • The prototype defines common implementation

Each one of these points requires more explanation than just that – thus the 2 hours we spent discussing it. But once you’ve wrapped your head around these few ideas, you can effectively code in JavaScript, because you’ll know how JavaScript thinks. You’ll be Thinking in JavaScript.

The slides for this evening’s discussion can be found here. The source code for the evening was mostly pulled either from the slides themselves and copied into FireBug, or we made it up on the spot. We also discussed jQuery and referenced tutorials available from 15 days of jQuery.

I asked briefly in the midst of the demo if anyone would like to see more jQuery stuff.  Just about every hand went up, so I’ve signed up to present a jQuery session at Desert Code Camp in 2 weeks.  That promises to be a ton of fun too.

If you have any questions, thoughts, suggestions, gripes, or kudos about ‘Thinking in JavaScript’ or you’d like some assistance in JavaScript development efforts, don’t hesitate to drop me a line.  The more we know about how to code effectively, the less spaghetti code there will be for us to clean up later…  :)