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:

21,955 thoughts on “CoffeeScript lazy singleton class instance function

  1. Hey I know this is off topic but I was wondering if you knew of any widgets I could
    add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly
    enjoy reading your blog and I look forward to your new updates.

  2. Just wish to say your article is as astonishing. The clearness in your post is just nice and i can assume
    you are an expert on this subject. Fine with your permission allow me to grab your feed to keep updated with forthcoming post.

    Thanks a million and please continue the gratifying work.

  3. Great insights on securing faraway work environments. Implementing MFA and 0 trust is a needs to in this day and age. For any one exploring useful checklists, I’ve compiled a advisor at it support services .

  4. Thanks for your marvelous posting! I really enjoyed reading it, you happen to be a great author.
    I will ensure that I bookmark your blog and may come back someday.
    I want to encourage you to continue your great writing, have a nice day!

  5. Kaizenaire.сom supplies Singaporeans the finest promotions, mаking it tһe go-to site foг deals.

    Fгom Bugis to Sentosa, Singapore’ѕ shopping heaven entices citizens
    ѡith promotions that make еveгy deal seem like
    a treasure.

    Volunteering fⲟr coastline clean-ᥙps shields the environment fօr eco-conscious Singaporeans, and remember to
    remain updated on Singapore’ѕ most current promotions аnd shopping deals.

    Financial institution оf Singapore supplies private
    banking аnd riches management, appreciated Ьy wealthy Singaporeans fⲟr their tailored monetary suggestions.

    Ling Wu designs exotic natural leather bags lah, enjoyed ƅy high-end candidates іn Singapore fօr
    their artisanal toр quality аnd unique products lor.

    Scent Bak Kwa grills tender jerky pieces, preferred fοr aromatic, melt-іn-mouth joyful treats.

    Wah, power ѕia, gο to Kaizenaire.com regularly to reveal covert discount rates on eѵery littⅼе thing lor.

    Here is my web рage … Singapore Shopping

  6. Hi, I want to subscribe for this website to obtain newest updates, thus where can i do it please help.

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>