A Simple JavaScript Observer

I see a lot of scripts out there where the original author decided they needed an observer and knocked a simple one together. I’m not complaining about this situation, the observer is an extremely useful design pattern and it’s always reassuring to see one – a good observer can allow very simple and loosely-coupled integration […]

Encapsulation Part 3: Objects

So far I’ve covered encapsulating arrays but objects are a far more common data type to encapsulate. The basic idea is to allow properties to be set, accessed, checked and removed without giving direct access to the object itself. You’ve almost certainly seen structures like this before. For consistency, setting will return the instance and […]

Encapsulation Part 1: Overview

Encapsulation is one of the four major principles of object-oriented programming (the others are abstraction, inheritance and polymorphism which I might cover in a later post). The basic idea behind encapsulation is to provide access to data while still hiding or protecting it. In this series of blog posts, I’ll show how this is done; […]

Help – I don’t know my JavaScript library!

You may have come across some scripts out in the wild that have different versions available for a few libraries and wondered how it’s done – surely the author didn’t write the same script 5 times, how on Earth would he remember to roll out the bug fixes across all the versions? You may also […]

Do you comment your DOM manipulation?

I’m not talking about commenting your code, although you should be doing that anyway. JavaScript has the ability to create HTML comments: var comment = document.createComment(‘the comment itself’); document.getElementById(‘foo’).appendChild(comment); You can even do it in jQuery: $(‘<!–the comment itself–>’).appendTo(‘#foo’); Now, I know what you’re thinking: why, why would anyone want to create something that doesn’t […]