As the following example , I would like to have nested methods into coffescript class (or native js).
class Child
constructor: (@id) ->
console.log "new child #{@id}"
animations:
start: ->
console.log @, "#{@id} start animation"
custom:
rotate: ->
console.log @, "#{@id} custom rotate animation"
class Parent
constructor: ->
@_childs = []
_.each [1..10], (index) =>
@_childs.push new Child(index)
startAll: ->
_.each @_childs, (child) ->
child.animations.start()
parent = new Parent()
parent.startAll()
The only way I found, is to
- Clone my nested methods object
- Bind each nested methods to my current object
I'd rather keep it into my prototype object, in order to avoid to recreate all the nested methods when I create a new object. I found an answer here, but I didn't find a nice way to deal with it. I also found the answer, is it the only way to do it ?
Thx for your help