CoffeeScript lazy singleton class instance function

Explanation

“@instance:” allows us to attach an object/value to the Foo class instead of Foo’s prototype. Recall that “@” in CoffeeScript refers to JavaScript’s “this”.

After “@instance:” there is a function that is immediately executed. This function returns a function, closed around a variable called instance. Both these functions have their context (their “this”) bound to the Foo object using CoffeeScript’s wonderful “define and bind” operator “=>” as opposed to the normal function definition operator “->”.

When called, the function that “@instance” ends up referring to simply checks to see if the “instance” variable is empty and if so, assigns a new instance of Foo to it. Newing up a Foo is done using the “@” symbol since the function’s “this” refers to the Foo object. It then returns the instance.

Output

The CoffeeScript above compiles down to this:

23,235 thoughts on “CoffeeScript lazy singleton class instance function

  1. What’s Happening i’m new to this, I stumbled upon this I have
    found It positively helpful and it has helped
    me out loads. I hope to give a contribution & assist other customers like
    its helped me. Great job.

  2. Do you mind if I quote a few of your articles as long as I provide
    credit and sources back to your site? My blog is in the
    exact same niche as yours and my users would
    certainly benefit from some of the information you present here.
    Please let me know if this ok with you. Cheers!

  3. Good day I am so delighted I found your site, I really
    found you by error, while I was looking on Digg for something else, Regardless
    I am here now and would just like to say kudos for a remarkable post and a all round
    thrilling blog (I also love the theme/design), I don’t have time
    to go through it all at the moment but I have bookmarked it
    and also added your RSS feeds, so when I have time I will be back to
    read a great deal more, Please do keep up the excellent job.

  4. We are a bunch of volunteers and opening a brand new
    scheme in our community. Your website offered us with
    valuable information to work on. You have performed a formidable process and our whole group might be grateful
    to you.

  5. An impressive share! I have just forwarded this onto a co-worker who was doing a little homework on this.
    And he actually bought me breakfast due to the fact
    that I discovered it for him… lol. So let me reword this….

    Thank YOU for the meal!! But yeah, thanks for spending the time
    to discuss this topic here on your site.

  6. Unquestionably believe that which you stated. Your favorite justification appeared to be
    on the internet the simplest thing to be aware of.
    I say to you, I certainly get annoyed while people consider worries that
    they just do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people can take a signal.
    Will probably be back to get more. Thanks