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. Valuable info. Lucky me I found your website accidentally, and I am stunned why this
    accident did not came about earlier! I bookmarked it.

  2. Wow, incredible blog structure! How long have you been running
    a blog for? you make blogging glance easy. The overall look of your web
    site is great, let alone the content material!

  3. Having read this I believed it was extremely enlightening.

    I appreciate you spending some time and effort to put this informative article together.
    I once again find myself personally spending
    a significant amount of time both reading and leaving comments.
    But so what, it was still worth it!

  4. Wonderful 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 further.
    Kudos!

  5. I really like your blog.. very nice colors & theme.
    Did you design this website yourself or did
    you hire someone to do it for you? Plz answer back as I’m looking to create my own blog and would like to know where u
    got this from. cheers

  6. I really love your blog.. Very nice colors & theme. Did you develop this website yourself?

    Please reply back as I’m attempting to create my
    very own blog and want to find out where you got this from
    or just what the theme is named. Appreciate it!

  7. I have learn a few good stuff here. Definitely worth bookmarking for revisiting.
    I surprise how a lot effort you place to create this type of great
    informative website.

  8. Your means of describing all in this article is really nice,
    all can without difficulty know it, Thanks a lot.

  9. Hmm is anyone else experiencing problems with the images on this blog loading?
    I’m trying to determine if its a problem on my end or if it’s the blog.
    Any feedback would be greatly appreciated.

  10. This is really interesting, You’re a very skilled blogger.
    I have joined your feed and look forward to seeking more of your great post.
    Also, I’ve shared your site in my social networks!

  11. It’s a pity you don’t have a donate button! I’d most certainly
    donate to this brilliant blog! I suppose for now i’ll
    settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to new updates and will talk about this website with
    my Facebook group. Chat soon!

  12. Hey there this is kind of of off topic but I was wanting to
    know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience.

    Any help would be enormously appreciated!

  13. I have been surfing online greater than 3 hours as of
    late, but I by no means found any fascinating article like yours.
    It’s beautiful worth sufficient for me. In my view, if all webmasters and bloggers made excellent content as you probably did, the internet will probably
    be a lot more helpful than ever before.

  14. Undeniably believe that which you said. Your favorite reason seemed
    to be on the internet the simplest thing to be aware of. I say to you,
    I certainly get irked while people consider worries that they plainly
    do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without
    having side-effects , people can take a signal.
    Will probably be back to get more. Thanks

  15. Penjelasan mengenai Situs Parlay Resmi mudah dipahami sehingga pembaca bisa mengetahui cara memanfaatkannya dengan baik.