読者です 読者をやめる 読者になる 読者になる

hayateasdf's blog

Unity,C#, javascript,C++,python,batなど

CoffeeScript class テスト

class Observer
  constructor: ->
    @listeners = {}

  on: (event, func) ->
    if !@listeners[event]?
      @listeners[event] = []
    @listeners[event].push(func)

  off: (event, func) ->
    for listener, i in @listeners[event]
      if listener == func
        @listeners.splice(i, 1)
  
  trigger: (event) ->
    return if !@listeners[event]?

    for listener in @listeners[event]
      listener()

viewMethod = (elem) ->
  console.log "-------method------"
  for k, v of elem
    if typeof v == "function"
      console.log "#{k}"
  console.log "--------------------"


observer = new Observer()
hello = -> console.log("hello world")
observer.on("hoge", hello)
observer.trigger("hoge")
viewMethod(observer)