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. Если на предзвонке выявляются «красные флаги» — давящая боль в груди, одышка в покое, спутанность сознания, судороги, неукротимая рвота, резкое падение насыщения кислородом — мы организуем бесшовный перевод в стационар. Место резервируем заранее, логистику строим так, чтобы не привлекать внимания соседей и не затягивать старт лечения. При отсутствии угроз терапия может начаться дома с последующим телесопровождением и при необходимости — краткой госпитализацией.
    Ознакомиться с деталями – частная наркологическая клиника в саратове

  2. hello!,I love your writing very much! percentage we keep up a correspondence more
    approximately your post on AOL? I need an expert on this area to unravel my
    problem. Maybe that’s you! Looking ahead to see you.

  3. Amazing blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements
    would really make my blog jump out. Please let me know
    where you got your design. Kudos

  4. It’s amazing to go to see this website and reading
    the views of all colleagues on the topic of this article, while I am also keen of
    getting familiarity.

    my homepage – AT99娛樂城

  5. My partner and I stumbled over here coming from a different
    web address and thought I might check things out. I like what I see so now i am following you.
    Look forward to checking out your web page for a second time.