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. Howdy! I know this is kinda off topic but I was wondering which blog
    platform are you using for this website? I’m getting fed up of WordPress because I’ve had problems with hackers and I’m looking at options for
    another platform. I would be fantastic if you could point me in the direction of a good platform.

  2. Hi, i read your blog from time to time and i own a similar one and i was
    just wondering if you get a lot of spam comments?
    If so how do you reduce it, any plugin or anything you can suggest?
    I get so much lately it’s driving me crazy so any support
    is very much appreciated.

  3. Everything is very open with a precise clarification of the issues.
    It was definitely informative. Your website is extremely helpful.
    Thanks for sharing!

  4. You are so awesome! I do not believe I’ve read anything like that before.
    So nice to discover another person with some genuine thoughts
    on this issue. Really.. thank you for starting this up.

    This website is one thing that is needed on the internet, someone with a bit of originality!

  5. Excellent post however I was wondering if you could
    write a litte more on this topic? I’d be very thankful if you could elaborate a little bit more.
    Appreciate it!

  6. I have been surfing online more than three hours today, yet
    I never found any interesting article like yours. It’s
    pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the web will be a lot
    more useful than ever before.

  7. Do you mind if I quote a few of your articles as long as I provide credit
    and sources back to your webpage? My blog is in the very same area of interest as yours and my users would genuinely
    benefit from some of the information you provide here.
    Please let me know if this alright with you.
    Many thanks!

  8. Definitely Hot Teen Bedroom Decorating Idea Hot colours can really make a room bounce.
    You’ll discover extra good ways to decorate your teen’s bedroom on the
    subsequent web page. Learn how to keep your teen’s room trendy and practical.

  9. Nice post. I learn something totally new and challenging
    on websites I stumbleupon on a daily basis. It will always be interesting to read articles from other writers and use something from other websites.