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. BES4D adalah bandar slot4d resmi terpercaya dengan koleksi slot 4d lengkap,
    bonus melimpah, transaksi cepat, dan keamanan terjamin pengalaman terbaik dan peluang jackpot terbesar.
    Dunia permainan slot online kini semakin populer di Indonesia, dan hadirnya bes 4d menjadi jawaban bagi
    para pemain yang mencari situs resmi, aman, serta terpercaya.

  2. Great blog here! Also your website loads up fast!

    What host are you using? Can I get your affiliate
    link to your host? I wish my website loaded up as
    quickly as yours lol

  3. Hello, i read your blog occasionally and i own a similar one and i
    was just curious if you get a lot of spam feedback? If
    so how do you reduce it, any plugin or anything you can advise?
    I get so much lately it’s driving me insane so any support is very
    much appreciated.

  4. I am extremely impressed with your writing skills as
    well as with the layout on your blog. Is this a paid
    theme or did you modify it yourself? Either way keep up the
    excellent quality writing, it’s rare to see a great blog like
    this one these days.

  5. Sweet blog! I found it while searching on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Cheers

  6. These are really great ideas in concerning blogging.
    You have touched some fastidious factors here. Any
    way keep up wrinting.

  7. Slot88resmi provider agen slot88 resmi dengan lisensi paling gacor dan terpercaya login di situs
    slot88resmi.com untuk promo bonus tanpa batas. Dunia permainan slot online
    semakin berkembang pesat dalam beberapa tahun terakhir.
    Kehadiran berbagai platform permainan digital membawa pengalaman baru bagi para penggemar judi online di seluruh dunia, termasuk Indonesia.

  8. After checking out a few of the blog articles on your web
    page, I seriously appreciate your way of writing a blog.
    I bookmarked it to my bookmark site list and will
    be checking back soon. Take a look at my web site as well and let me know your opinion.

  9. Great goods from you, man. I have understand your stuff previous to and you are just
    extremely excellent. I really like what you’ve acquired here, certainly like what you’re
    stating and the way in which you say it. You make it entertaining and you still care for to keep it smart.

    I cant wait to read far more from you. This is actually
    a tremendous site.

  10. My spouse and I absolutely love your blog and find most of your post’s to be just what I’m looking for.

    Does one offer guest writers to write content in your
    case? I wouldn’t mind composing a post or elaborating on a few of the subjects you write regarding here.
    Again, awesome web log!