Clazz
Yet another attempt to implement classes as a DSL.
The difference:
- super is @super (or supr, in the class body)
- use
init
, notconstructor
- Method binding looks like this: boundMethod$: -> @ != GLOBAL
The benefits:
- Super no longer depends on lexical scope -- you can use @super anywhere, as long as you specify the method name (as in Python).
- Metaprogramming is easier, for example dynamically creating bound methods is as easy as appending a '$' to the method name.
- Convenient getter/setter syntax.
Declare a class Foo
Foo = clazz 'Foo', null, (supr) ->
@myStaticMethod = -> # is set on Foo
init: ->
console.log "Foo initializer"
foo$: -> # a bound method
console.log "@", @, "@barProp", @barProp, "instanceof Foo?", @ instanceof Foo
bar$: get: -> "bar property getter" # a property
myClassMethod: => # this is bound to the class
Extending Foo:
Bar = clazz 'Bar', Foo, (supr) ->
barProp: 'barProp!'
init: (@bar='init:bar') ->
@super.init()
console.log "Bar initializer"
moo$: ->
console.log "@", @, "@constructor", @constructor, "@super", @super
boo: -> # an unbound method
console.log "@", @, "@constructor", @constructor, "@super", @super