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. Каждая процедура проводится под контролем квалифицированного врача. При необходимости возможен экстренный выезд специалиста на дом, что позволяет оказать помощь пациенту в привычной и безопасной обстановке.
    Детальнее – https://vyvod-iz-zapoya-odincovo6.ru/vyvod-iz-zapoya-kruglosutochno-v-odincovo/

  2. It’s appropriate time to make a few plans for the future and it is time to be happy.
    I have read this publish and if I may I want to counsel you some
    fascinating things or advice. Perhaps you could write subsequent articles relating to this article.
    I desire to read more things about it!

  3. Эта информационная заметка содержит увлекательные сведения, которые могут вас удивить! Мы собрали интересные факты, которые сделают вашу жизнь ярче и полнее. Узнайте нечто новое о привычных аспектах повседневности и откройте для себя удивительный мир информации.
    Посмотреть всё – https://www.gasthaus-baule.de/2019/03/25/exploring-street-food-in-bangkok

  4. If some one wants expert view about blogging after that i propose him/her
    to go to see this webpage, Keep up the fastidious
    job.

    Take a look at my blog post: justin woll

  5. Hello would you mind stating which blog platform you’re working with?
    I’m planning to start my own blog in the near future but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique.
    P.S Apologies for being off-topic but I had to ask!

    Also visit my blog: Jet Ski Repair Jacksonville

  6. Эта статья полна интересного контента, который побудит вас исследовать новые горизонты. Мы собрали полезные факты и удивительные истории, которые обогащают ваше понимание темы. Читайте, погружайтесь в детали и наслаждайтесь процессом изучения!
    Жми сюда — получишь ответ – https://claragrau.com.ar/case-studies/the-journey-of-life-understanding-transits