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. I read this paragraph fully about the comparison of most
    recent and earlier technologies, it’s amazing article.

  2. I will right away grasp your rss feed as I can’t in finding your email subscription hyperlink or e-newsletter service.
    Do you have any? Kindly let me realize so that I could
    subscribe. Thanks.

  3. Paragraph writing is also a excitement, if you know then you can write or else it is complex to write.

  4. It’s hard to find well-informed people for this topic, but you sound like you know what you’re talking about!
    Thanks

  5. With havin so much content do you ever run into any issues
    of plagorism or copyright infringement? My site has a lot of unique content I’ve either created myself or outsourced but it seems a lot of it is popping it up all over the internet without my authorization. Do you know any techniques
    to help stop content from being stolen? I’d really
    appreciate it.

  6. Definitely consider that that you said. Your favourite reason seemed to be on the
    internet the easiest thing to keep in mind of. I say
    to you, I definitely get annoyed whilst folks think about issues that they plainly do not recognize
    about. You controlled to hit the nail upon the top and outlined out the whole thing without having side-effects , other people could take
    a signal. Will likely be again to get more. Thanks