Mixins
Learn how to create and apply mixins
Overview
A mixin is an entity that captures reusable data properties and methods, but cannot be instantiated. The reason we have mixins is because the our object model uses single inheritance—meaning a class can only have one class as its base class. If we want multiple classes to share data properties and methods from multiple entity types, then it is not possible.
Creating a Mixin
You create a mixin using the Mixin.create()
method. The create method takes a definition hash that contains data properties and methods, which is similar to how you created a class.
Using a Mixin
Applying Mixin to Class Definition
The extend()
method takes optional list of mixins before the class definition.
For example, let's assume we want to mix in the Events
mixin with the Person
class. We can do that by preceding the definition with the mixin.
Now, anytime we create a Person
class, it will have access to the methods and data properties defined in the Events
mixin.
Applying Mixin to Object Instance
Similar to the extend()
method, the create()
method takes optional list of mixins that before the class definition.
For example, let's assume we want to mix in the Events
mixin with the Person
class. We can do that by preceding the definition with the mixin.
Now, only the president
instance will have access to the methods and data properties defined in the Events
mixin.
Last updated