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. Wow! This blog looks exactly like my old one!
    It’s on a entirely different topic but it has pretty much the same layout and design. Outstanding choice of colors!

  2. What’s up, I log on to your new stuff regularly. Your story-telling
    style is witty, keep doing what you’re doing!

  3. I really love your website.. Pleasant colors & theme.
    Did you make this website yourself? Please reply
    back as I’m hoping to create my own site and would love to
    find out where you got this from or what the theme is called.
    Thank you!

  4. Do you mind if I quote a few of your posts as long as I provide
    credit and sources back to your weblog? My blog is in the very same area of interest as yours and my visitors would certainly benefit from a lot of the information you present here.
    Please let me know if this okay with you. Appreciate it!

  5. Oh my goodness! Amazing article dude! Thank you so much, However I am going through problems with your RSS.

    I don’t know the reason why I am unable to subscribe to it.
    Is there anybody else having similar RSS problems? Anyone that knows the answer will you kindly respond?
    Thanx!!

  6. Оформите займ https://zaimy-63.ru онлайн без визита в офис — быстро безопасно и официально. Деньги на карту за несколько минут круглосуточная обработка заявок честные условия и поддержка клиентов 24/7.

  7. В этом информативном тексте представлены захватывающие события и факты, которые заставят вас задуматься. Мы обращаем внимание на важные моменты, которые часто остаются незамеченными, и предлагаем новые перспективы на привычные вещи. Подготовьтесь к тому, чтобы быть поглощенным увлекательными рассказами!
    Детали по клику – https://care.org.tl/product/sethunya