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,869 thoughts on “CoffeeScript lazy singleton class instance function

  1. Yesterday, while I was at work, my cousin stole my apple ipad and tested to see if it can survive
    a 30 foot drop, just so she can be a youtube sensation. My apple
    ipad is now broken and she has 83 views. I know this is entirely
    off topic but I had to share it with someone!

  2. I don’t know whether it’s just me or if everybody else encountering issues with your
    website. It appears as though some of the written text
    in your posts are running off the screen. Can somebody else please provide feedback and let me know
    if this is happening to them as well? This might be a issue with my web browser because I’ve had this happen before.
    Thank you

  3. This article does wonders towards educating readers on crucial factors influencing durability related specifically toward modern-day residential setups – inquire today through ### anyKeyWord ## Commercial roofing

  4. Having read this I thought it was extremely enlightening.
    I appreciate you finding the time and energy to put this informative article together.

    I once again find myself personally spending way too
    much time both reading and leaving comments. But so what, it was still worth it!

  5. 1. Eindelijk een casino gevonden met een eerlijke no deposit bonus! Vaak zijn die dingen zo vaag, maar hier kun je echt wat winnen zonder eerst geld in te zetten. Echt chill voor mensen die willen proberen zonder risico.

    2 U kunt meer te weten komen

  6. Look, as a small biz owner, I found setting up an HRA super helpful—it lets employees get reimbursed for health expenses tax-free. Also, joining my trade association gave me access to group rates I couldn’t get solo Get more info

  7. Somebody necessarily lend a hand to make severely articles I’d state.
    That is the very first time I frequented your web page and thus far?
    I amazed with the research you made to make this actual post extraordinary.
    Wonderful task!

  8. Excellent goods from you, man. I’ve be mindful your stuff previous to and you’re just extremely great.
    I really like what you have received here, certainly like what you’re saying and
    the way in which in which you assert it.

    You’re making it entertaining and you still take care of to stay it smart.
    I can not wait to learn far more from you. This is really a terrific web site.

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>