Private members in coffeescript and the query string

Because coffeescript is really just a thin veneer over javaScript there are, in my opinion, a few lost opportunities. For example, when you create a class all members (properties or functions) are public. However, there are work arounds.

I created a class to help me work with query string parameters. Actually, I ended up creating two classes, one an implementation class that did most of the hard work, and another to act as the public interface to the rest of the world. The “public” class is attached to the window object to make it globally available within the browser, the implementation class will only be available to other code inthe same script as the script as a whole will be wrapped by the coffeescript compiler in an anonymous function.

The publically available class, the one attached to the window object exposes three functions, yet it doesn’t expose its association to the implementation class. I achieved this by making the the association to the implementation class inside the constructor and then definining all the other functions in the construtor too. Each method I wanted to make public I prefixed with the @ symbole (the equivalent of this. so that it would be available to users of the class

# Define the queryString class, attaching it to the window object,
# effectively making it globally available.
class window.queryString
  constructor: (url) ->
    # The constructor defines a local variable to hold the reference
    # to the implementation class. This acts as a private member variable.
    __qsImpl = new queryStringImpl(url)

    # The public methods are defined on "this" by using the @ prefix
    # They can access the local variables in the parent scope because
    # they are closures.
    @getValue = (key) ->
      result = __qsImpl.params[key]
      if not result?
        result = __qsImpl.canonicalParams[key.toLowerCase()]
      return result

    @count = () ->

    @hasKey = (key) ->
      return key of __qsImpl.params || key.toLowerCase() of __qsImpl.canonicalParams

The implementation class also detects when the url is null or undefined and automatically finds the url and extracts the query string information for you. With this you can then get access to the query string parameters by using code like this:

# Create the query string object
qs = new queryString()

# Get a value from the query string
theValue = qs.getValue("SomeQueryStringKey")

# Work out if a value is available on the query string
isAvailable = qs.hasKey("SomeQueryStringKey")

# Find the number of query string parameters
numberOfParams = qs.count()


  1. Jeff says:


    I found your post while investigating if coffeescript to determine if I should consider it for my library of jquery plugins that I am about to develop.

    I have looked at a ton of articles about OOJavascript and browsed thru the approaches of a number of micro-frameworks looking for a OO model to standardize on. It appears you are showing that coffeescript is not an end-all answer to OO js.

    Since you have experience and I appreciate the thought that you put into this to handling private members, what would be your advice on adopting Coffeescript for my new set of UI plugins? These plugins will be using jQuery, which I know quite well, but will also have elements that go beyond DOM manipulation and will perform calculations and communication i/o tasks. I want to use an OO concept for the library in general.

    Thanks for any help you might provide…

    1. Colin Mackay says:

      Interesting question. You might want to read this article on using coffeescript vs javascript to help you decide. It gives a better comparison between the pros-and-cons of each than I’ve put together.

      My gut feeling, however, is that coffeescript is a better language. It shields you from the bad bits of javascript (although doesn’t shield you from understanding javascript in the first place). From an OO perspective coffeescript is constrained by what javascript offers (so I don’t think you are going to get anything better in javascript itself, even although the subject of the post above is a bit of a hack).

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s