Functional Mixins
Eric Elliott

I didn’t see it mentioned, but a consideration point of functional mixins (and get/set/mutate closures around a state variable) are creating a new function per instance of the object.

I find that when I want to have a collection of static methods that mutate across the state of an object, a class is fairly well suited. For example, if we had some “Structure”, then we might have:

doSomething(structure, ...args) => newStructure
doSomethingElse(structure, ...args) => newStructure
doAThirdThing(structure, ...args) => newStructure

If these are intended to be “incremental” changes, it might be nice to use a class to have a “working copy” of that data, even if it is immutable.

class MyMutator {
this.state = state;

doSomething(...args) {
// do logic, immutably changing this.state
return this;

doSomethingElse(...args) {}
  doAThirdThing(...args) {}

The benefit here is more an API than anything else — maybe incremental operations on the state can be improved with some caching or indexing structures. Anyone can grab “state” to get the at-a-moment value. As long as the class is immutably changing its state, you get a lot of the benefits without creating multiple closures per instance.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.