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’m not sure exactly why but this weblog is loading extremely slow for me.
    Is anyone else having this problem or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  2. Oh my goodness! Impressive article dude! Thank you so much, However I
    am experiencing difficulties with your RSS. I don’t understand the reason why I am unable
    to join it. Is there anyone else getting identical RSS issues?
    Anyone that knows the answer will you kindly respond? Thanx!!

  3. Definitely believe that which you stated. Your favorite justification seemed to
    be on the net the simplest thing to be aware of.
    I say to you, I certainly get irked while people consider worries that
    they just do not know about. You managed to hit the
    nail upon the top and defined out the whole thing without having
    side-effects , people can take a signal. Will probably be back to get more.
    Thanks

  4. Unquestionably believe that which you said. Your favorite
    reason seemed to be on the net the simplest thing to be aware of.
    I say to you, I certainly get annoyed while people consider worries that
    they plainly don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without
    having side effect , people can take a signal. Will probably be back to get
    more. Thanks

  5. A person essentially assist to make seriously articles I would
    state. That is the first time I frequented your website page and up to now?
    I surprised with the research you made to create this actual put up amazing.
    Great activity!

  6. Good day! I know this is kinda off topic but I was wondering if you knew where I could find a
    captcha plugin for my comment form? I’m using the same blog platform as yours and
    I’m having problems finding one? Thanks a lot!

  7. I am not certain where you are getting your info, however good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for wonderful information I was searching for
    this information for my mission.

  8. I truly love your blog.. Very nice colors & theme. Did you build this amazing
    site yourself? Please reply back as I’m trying to
    create my own personal site and would like to learn where
    you got this from or exactly what the theme is named. Kudos!