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. Its like you read my mind! You appear to know so much about this, like you wrote the
    book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is
    wonderful blog. A great read. I will definitely be back.

  2. Hi would you mind sharing which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a tough time
    making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and
    I’m looking for something completely unique.
    P.S My apologies for being off-topic but I had to ask!

  3. I was recommended this website by my cousin. I’m not sure whether this post is written by him as
    no one else know such detailed about my difficulty.
    You’re incredible! Thanks!

  4. Thank you for the good writeup. It in fact
    was a amսsement accoᥙnt it. Look aԁvanced to more added
    agreeable from you! However, һow could we communicate?

  5. คอนเทนต์นี้ อ่านแล้วเข้าใจเรื่องนี้มากขึ้น ครับ
    ผม เพิ่งเจอข้อมูลเกี่ยวกับ เรื่องที่เกี่ยวข้อง
    สามารถอ่านได้ที่ Chau
    น่าจะเป็นประโยชน์กับหลายคน
    เพราะให้ข้อมูลเชิงลึก
    ขอบคุณที่แชร์ คอนเทนต์ดีๆ นี้
    หวังว่าจะมีการอัปเดตเนื้อหาเพิ่มเติมเร็วๆ นี้

  6. Everything posted made a lot of sense. But, think on this, suppose you added a
    little information? I am not saying your information is not good,
    however what if you added a post title that grabbed people’s attention? I mean CoffeeScript lazy singleton class instance function | freestyle developments is kinda plain. You should look at Yahoo’s home page and see how they create post headlines to get viewers to open the links.
    You might add a related video or a related picture or
    two to get readers excited about everything’ve got to
    say. In my opinion, it might bring your website a little livelier.

  7. Hi! This post couldn’t be written any better! Reading through this
    post reminds me of my previous room mate! He always kept chatting about this.
    I will forward this article to him. Pretty sure he will have a good read.
    Thank you for sharing!

  8. Pretty nice post. I just stumbled upon your weblog and wanted to say that I
    have truly enjoyed browsing your blog posts. In any case I will be subscribing to your feed and I hope you write again very soon!

  9. Good day! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the same niche.
    Your blog provided us useful information to work on. You have done
    a marvellous job!

  10. Thank you for any other magnificent article. The place else may anyone get that
    kind of info in such an ideal method of writing?
    I have a presentation next week, and I am at the look for such info.

  11. WeЬsite Animsaga menyeɗiakan sitսs streɑming anime suЬtitle Indonesia.

    Nіkmati updɑte anime setiap hari hanya di Animsaga.

  12. Everyone loves what you guys are up too. This sort of clever work and coverage!
    Keep up the fantastic works guys I’ve incorporated you guys to my own blogroll.

  13. Currently it appears like Movable Type is the top blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?