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:

21,099 thoughts on “CoffeeScript lazy singleton class instance function

  1. I’m truly enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much
    more pleasant for me to come here and visit more often. Did you
    hire out a developer to create your theme? Outstanding work!

  2. It is perfect time to make some plans for the future and it is time to be
    happy. I’ve read this post and if I could I wish to suggest you few interesting things or suggestions.
    Maybe you can write next articles referring to this article.
    I wish to read even more things about it!

  3. Hey I am so excited I found your weblog, I really found you by accident, while
    I was researching on Yahoo for something else, Nonetheless
    I am here now and would just like to say cheers for a incredible post and a
    all round interesting blog (I also love the theme/design),
    I don’t have time to read it all at the moment but
    I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent job.

  4. Please let me know if you’re looking for a
    author for your site. You have some really great articles and I feel I would be a good
    asset. If you ever want to take some of the load off, I’d absolutely love to write some material for
    your blog in exchange for a link back to mine.

    Please send me an email if interested. Cheers!

  5. I used to be recommended this website via my cousin. I’m no longer positive whether
    this publish is written through him as no one else recognise such unique approximately my difficulty.
    You’re wonderful! Thank you!

  6. Hello there! I know this is kinda off topic however , I’d figured I’d
    ask. Would you be interested in exchanging links or maybe
    guest writing a blog article or vice-versa? My site discusses a lot of the same topics as yours and I feel we could
    greatly benefit from each other. If you might be interested
    feel free to shoot me an email. I look forward to hearing from
    you! Terrific blog by the way!

    My page: real estate agent St. George LA

  7. ”#CommunityBuilding exercises planned resulted positively beyond expectations—all credit again belongs towards recommendations extended mutually agreed upon while engaging through:###!” SEO Expert Seattle WA

  8. Oh my goodness! Impressive article dude! Thank you,
    However I am having troubles with your RSS. I don’t know why
    I am unable to join it. Is there anybody else getting the same RSS
    issues? Anyone who knows the answer can you kindly respond?
    Thanks!!

  9. Thank you a bunch for sharing this with all folks you
    actually understand what you’re talking about! Bookmarked.
    Kindly also discuss with my website =). We can have a hyperlink exchange arrangement among us

  10. Khi nhắc đến văn hóa ẩm thực nơi đây thì chắc chắn mọi người đều phải nhắc tới các món trong mâm lễ vật!… Mong rằng mọi người cũng yêu thích như mình!… Bạn hãy xem chi tiết ngay nhé qua link dưới đây là : ## dịch vụ đồ cúng tp hcm

  11. Heya this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise so
    I wanted to get advice from someone with experience.
    Any help would be enormously appreciated!

  12. It’s going to be finish of mine day, except before end I
    am reading this impressive paragraph to improve
    my experience.

  13. Hi there! Someone in my Facebook group shared this website with
    us so I came to look it over. I’m definitely loving the information. I’m bookmarking and will be
    tweeting this to my followers! Superb blog and excellent design and style.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>