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 blog frequently and I genuinely appreciate your
    content. The article has truly peaked my interest.
    I’m going to take a note of your blog and keep checking for new details about once per week.
    I opted in for your RSS feed too.

  2. Hello there! This is my 1st comment here so I just wanted to give a quick shout out and say I
    genuinely enjoy reading your blog posts. Can you recommend any other blogs/websites/forums that deal with the same subjects?
    Appreciate it!

    My web-site; zambilica01

  3. Excellent pieces. Keep writing such kind of information on your page.
    Im really impressed by your site.
    Hey there, You have performed a fantastic job. I will certainly digg it and in my opinion recommend
    to my friends. I’m confident they will be benefited from this web
    site.

  4. Enjoy licensed, simple and secure financial trading platform
    with markets.com. Trade CFD on Forex, Shares, Commodities,
    Indices, ETFs, and more now!

  5. Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just
    sum it up what I wrote and say, I’m thoroughly enjoying your
    blog. I too am an aspiring blog writer but I’m
    still new to the whole thing. Do you have any tips and hints
    for beginner blog writers? I’d definitely appreciate it.

  6. Does your website have a contact page? I’m having a tough time
    locating it but, I’d like to shoot you an email. I’ve got some suggestions for
    your blog you might be interested in hearing. Either way, great website and I look forward
    to seeing it develop over time.

  7. Hello, i feel that i noticed you visited my blog thus i came to return the prefer?.I
    am trying to to find issues to improve my website!I suppose its good enough to use a few of your concepts!!

  8. It’s a pity you don’t have a donate button! I’d definitely donate to
    this excellent blog! I suppose for now i’ll
    settle for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will talk about this site with my Facebook group.
    Chat soon!