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:

15,693 thoughts on “CoffeeScript lazy singleton class instance function

  1. Nice blog right here! Additionally your web site lots up very fast!
    What host are you the usage of? Can I am getting
    your associate link for your host? I wish my website loaded
    up as fast as yours lol

  2. ”#FinancialPerformance metrics analyzed revealed vast improvements attributable solely via contributions directed throughout partnerships established around interaction facilitated via consultations held amongst teams operating within:###!” Local SEO Seattle CA

  3. Attractive part of content. I just stumbled upon your blog and
    in accession capital to assert that I get actually loved account your blog posts.
    Any way I’ll be subscribing to your feeds or even I success you
    get admission to constantly quickly.

  4. 4M Dental Implant Center San Diego
    5643 Copley Ⅾr ste 210, San Diego,
    ⅭA 92111, United Ѕtates
    18582567711
    whitе smile (Sara)

  5. Cannot wait see what unfolds next excited possibilities lie ahead bright shimmering future lighting way illuminating paths guiding footsteps leading onwards upwards towards achieving highest potentials realized manifesting miracles journeys traveled top roofing experts in Garfield

  6. Thank you for every other great article. Where else
    could anyone get that kind of info in such a perfect means of writing?
    I have a presentation next week, and I am at the search for such
    info.

  7. Hi, i read your blog occasionally and i own a similar one and i was
    just curious if you get a lot of spam remarks? If so how do you stop it,
    any plugin or anything you can recommend? I get so much lately it’s driving me mad so any help is very much appreciated.

  8. Петербургский темп жизни — поздняя занятость переменная нагрузка сезонные перепады света — усложняет восстановление после эпизодов употребления. Поэтому мы разработали модульные программы способные гибко подстраиваться под график и биоритмы: у кого-то «трудные часы» приходятся на поздний вечер на Васильевском острове у кого-то — на раннее утро перед сменой в Адмиралтейском районе. Маршрут строится вокруг человека а не вокруг расписания кабинета: очные приёмы немаркированные выезды на дом дневной стационар защищённые онлайн-сессии — всё объединено единым клиническим планом и общими целями. Подробнее – http://narkologicheskaya-klinika-v-spb14.ru

  9. This design is incredible! You definitely know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job.
    I really loved what you had to say, and more than that, how you presented it.
    Too cool!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>