class Foo | |
@instance: (=> | |
instance = null | |
=> | |
instance = new @() if not instance | |
instance | |
)() | |
singletonFoo = Foo.instance() |
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:
var Foo, singletonFoo; | |
Foo = (function() { | |
var _this = this; | |
function Foo() {} | |
Foo.instance = (function() { | |
var instance; | |
instance = null; | |
return function() { | |
if (!instance) { | |
instance = new Foo(); | |
} | |
return instance; | |
}; | |
})(); | |
return Foo; | |
}).call(this); | |
singletonFoo = Foo.instance(); |