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. I do agree with all of the ideas you have offered in your post.
    They’re very convincing and can definitely work.

    Still, the posts are too quick for beginners.
    Could you please lengthen them a bit from next time? Thanks for the post.

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

  3. I’m impressed, I must say. Rarely do I come across a blog
    that’s both equally educative and interesting, and let me
    tell you, you’ve hit the nail on the head. The problem is something that too few
    men and women are speaking intelligently about.
    I am very happy that I came across this during my hunt
    for something regarding this.

    Feel free to surf to my web site; a knockout post

  4. Hey there! I could have sworn I’ve been to this blog before but after checking through some of
    the post I realized it’s new to me. Anyhow, I’m definitely glad I found it and I’ll be bookmarking
    and checking back frequently!

  5. It’s actually very complex in this busy life to listen news on Television, thus I just use internet
    for that purpose, and get the most up-to-date news.

  6. Great goods from you, man. I have understand your
    stuff previous to and you are just extremely wonderful.
    I actually like what you’ve acquired here, really like what you’re saying and the way in which you say it.
    You make it entertaining and you still take care of to keep
    it sensible. I can’t wait to read much more from you. This is really a wonderful site.

  7. My brother suggested I might like this web site.
    He was totally right. This post actually made my day.
    You cann’t imagine just how much time I had spent for this information! Thanks!

  8. Spot on with this write-up, I really feel this web site needs
    far more attention. I’ll probably be back again to read through more, thanks
    for the advice!