A CoffeeScript class is a function definition with a bunch of class-ish stuff layered on top. This means that you can put pretty any code you want inside a class. So if we look at this:
class C
f = -> #...
f(6)
then we have a private function, f
, and a simple function call, f(6)
.
Now if we add a =>
in the mix:
class C
f = => #...
then f
becomes bound to the class so that @
inside f
will be C
itself.
Now we can apply that to the class in question:
class PropertyContainer
#...
get = (props) =>
@::__defineGetter__ name, getter for name, getter of props
set = (props) =>
@::__defineSetter__ name, setter for name, setter of props
get self: -> @_data.self or null
So we have two functions, get
and set
, that are abound to PropertyContainer
so that @
inside get
and set
is PropertyContainer
. Then we have a call to the get
function and adding some parentheses and braces to highlight the structure:
get({ self: (-> @_data.self or null ) })
we should start to see what's going on. The get self ...
is just a call to the get
function with an object parameter, that object has a single key (self
) whose value is a function.
That makes the whole get self ...
call look like this:
PropertyContainer::__defineGetter__('self', -> @_data.self or null)
Then if we look at what __defineGetter__
does:
// Non-standard and deprecated way
var o = {};
o.__defineGetter__("gimmeFive", function() { return 5; });
console.log(o.gimmeFive); // 5
we see that we're just setting up o.self
as an accessor alias for o._data.self
so that this:
o = new PropertyContainer(...)
o.self
is really the same as:
o = new PropertyContainer(...)
o._data.self
or more generally, the get
function allows you to send a simple property access to a function. You should also note the "standard compliant" examples from the MDN page:
// Using the get operator
var o = { get gimmeFive() {return 5}};
console.log(o.gimmeFive); // 5
Now we see that get
inside PropertyContainer
is emulating JavaScript's get
operator using the non-standard __defineGetter__
function.
The set
function inside PropertyContainer
similarly emulates JavaScript's set
operator.