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. Your means of describing all in this article is in fact nice,
    every one be able to simply be aware of it, Thanks a lot.

  2. Howdy, i read your blog from time to time and i own a similar one and i
    was just curious if you get a lot of spam comments?
    If so how do you protect against it, any plugin or anything you can recommend?
    I get so much lately it’s driving me insane so any help is very much appreciated.

  3. It’s actually a great and useful piece of info.
    I’m glad that you shared this useful information with us.
    Please keep us up to date like this. Thank you for sharing.

  4. hello!,I like your writing so so much! percentage we keep up a correspondence more
    about your article on AOL? I need a specialist in this area to resolve my problem.
    Maybe that is you! Taking a look ahead to peer you.

  5. I believe this is among the such a lot vital information for me.
    And i’m happy studying your article. But should statement on some normal issues,
    The website style is ideal, the articles is in reality nice : D.
    Good task, cheers

  6. Greetings! I know this is somewhat 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 think what you said was very logical. But, consider this, what if you were to create a awesome post title?
    I mean, I don’t want to tell you how to run your blog, but suppose you added a post title that makes people want more?

    I mean CoffeeScript lazy singleton class instance function | freestyle
    developments is a little vanilla. You ought to glance at Yahoo’s
    front page and watch how they create article titles to grab people to click.

    You might add a video or a related picture or two to grab readers excited about everything’ve
    got to say. In my opinion, it might make your posts a little livelier.

  8. I loved as much as you’ll receive carried out right here.
    The sketch is tasteful, your authored subject matter stylish.
    nonetheless, you command get got an impatience over
    that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly a lot often inside case you shield this hike.

  9. It’s going to be ending of mine day, however before ending
    I am reading this fantastic article to increase my knowledge.