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. Pembahasan tentang Situs Mix Parlay komprehensif.

    Banyak pemula yang bingung soal mekanisme taruhan kombinasi, tapi penjelasan di artikel ini membuatnya terasa lebih sederhana.

  2. Hey there! Quick question that’s completely off topic.
    Do you know how to make your site mobile friendly? My
    weblog looks weird when viewing from my iphone4.

    I’m trying to find a template or plugin that might
    be able to resolve this problem. If you have any recommendations, please share.
    With thanks!

  3. You really make it appear really easy with your presentation but
    I find this topic to be actually something which I believe I would
    by no means understand. It sort of feels too complicated and extremely huge for me.

    I’m looking forward to your subsequent post,
    I will attempt to get the grasp of it!

  4. I believe everything said made a lot of sense.

    But, what about this? suppose you added a little information? I ain’t suggesting your information isn’t
    good, but what if you added something to maybe grab people’s attention?
    I mean CoffeeScript lazy singleton class instance function | freestyle developments is a little vanilla.
    You should look at Yahoo’s home page and watch how they create news headlines to grab people to open the
    links. You might try adding a video or a related pic or two to get people interested about what you’ve written. In my opinion,
    it might bring your blog a little livelier.