cardamom

0.0.9d • Public • Published

Clazz

Yet another attempt to implement classes as a DSL.

The difference:

  1. super is @super (or supr, in the class body)
  2. use init, not constructor
  3. Method binding looks like this: boundMethod$: -> @ != GLOBAL

The benefits:

  1. Super no longer depends on lexical scope -- you can use @super anywhere, as long as you specify the method name (as in Python).
  2. Metaprogramming is easier, for example dynamically creating bound methods is as easy as appending a '$' to the method name.
  3. 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

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i cardamom

    Weekly Downloads

    17

    Version

    0.0.9d

    License

    none

    Last publish

    Collaborators

    • jaekwon