Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    acts-aspublic

    acts-as

    a simple class-level mixin module for Node.JS, which provides a handy "composition over inheritance" for writting complex programs

    How To Use

     
    require "acts_as"
     
    class A
     
      this.toString = ->
        'ClassA'
     
      # after mixed in, use this method to detect who behaves like who (duck typing) 
      this.isA = (obj) ->
        return if obj? then Boolean(obj["__is#{@}"]) else false
     
      a: ->
        console.log "[A.a] called"
        "AAA"
     
    class B
      b: ->
        console.log "[B.b] called"
        "BBB"
     
    class C
      @acts_as A,B
      c: ->
        console.log "[C.c] called"
        "CCC"
     
    = new C
    console.dir i
    console.log "i.a():#{i.a()}"
    console.log "i.b():#{i.b()}"
    console.log "i.c():#{i.c()}"
    console.log "i behave like A? : #{A.isA(i)}"
     

    Reference

    This little module is inspired by:

    1. discussion on coffee-script mixin: https://github.com/jashkenas/coffee-script/issues/452#issuecomment-3699651

    2. acts_as in the Ruby's way: http://yehudakatz.com/2009/11/12/better-ruby-idioms/

    Keywords

    none

    install

    npm i acts-as

    Downloadsweekly downloads

    1

    version

    0.1.0

    license

    none

    repository

    github.com

    last publish

    collaborators

    • avatar