I realized I don't know how to do something I think is pretty important, which is to create a 'subclass', or in other words a nested object upon an instance which has functions which refer to the instance.
For example, I would like to be able to do this:
sequence = new Sequencer(2, [true, true])
sequence.is_valid # returns true or false
using the following code:
class Sequencer
Validations:
correct_sequence_length: ->
@division == @sequence.length
positive_length: ->
@division > 0
constructor: (args) ->
{ @sequence, @division } = args
is_valid: ->
@Validations.correct_sequence_length() &&\
@Validations.positive_length()
However it seems that since Validations
is an object, it has its own scope and the this
in its methods doesn't refer to the instance, but rather the Validations
object. This means it can't access @sequence
or @tempo
. I also tried using an arrow function but it didn't work.
Is the best way to deal with this to make Validations
its own class?