Object-mixins

In this post we will see a small hands on example how to inject (or "mix-in") new functionality for the Object. This is done by adding two objects together. Basically what we are doing can be simplified with statement: {} + {}.

When to use this pattern?

This pattern can be used in the real settings if we change the input to something else than just fixed JSON. We could for example use the "file"-component, which reads all the files from the given folder and enriches the functionality for each read JSON-content. Or we could use the "httpserver"-component to enrich functionality for each request. The possibilities are endless.

Example

First, let's define a named-value $PersonFuncs, which is an object with two functions. The functions refer to object's "self", with "_" -notation.

In the Select-statement we mix the root-object with the $PersonFuncs -object, offering new behavior for the new object. Finally we invoke the "initials" -behavior.

Let's try to make the code as clean and readable as possible. Hence, here's the second way to do this, this time with References. As we remember, the References are special kinds of Lambda-functions which are auto-invoked on object-access.

Let's still continue making this example more readable. This time we move the $PersonFuncs into its own file, which we will then import for the main-query.

Now it looks very Operonic and we are happy :-)