Foibles
foible. A quirk, idiosyncrasy, or mannerism; unusual habit or way (usage is typically plural), that is slightly strange or silly. Wiktionary
Foibles is a library for composing JavaScript and TypeScript classes using a mixin pattern.
Foibles is available on NPM: npm install foibles
Creating mixins
Mixins are functions that creates a class that have a dynamic super class. This
makes it so that things as super
work as intended and that mixins can override
functions in their parent class.
; const SomeMixin = ;
For TypeScript you should also define the type, to enable you to build functions that consume any object with the mixin:
; ;
If you want to extend a specific class you can use typeof BaseClass
to do so:
Creating a base class
To create an extendable class call toExtendable
:
; const BaseType = ;
For TypeScript you should also define the type, to enable you to build functions that consume the base type:
; ;
Using mixins
BaseType
will be enhanced with a static with
function that provides
the mixin functionality. To sub class BaseType
and at the same time
use SomeMixin
:
{ // Allow super class to do stuff super; // doMixinStuff was provided via SomeMixin this; }
Use instanceof
to check if an object has a mixin:
const object = ;console;
Note: It's possible to use instanceof
only if Symbol.hasInstance
is supported.
Check compatibility at MDN
Creating a mixin depending on other mixins
This library supports a mixin to depend on other mixins by applying them as needed in the mixin function:
// Define the first mixinconst Debug = ; // Create a mixin that applies the Debug mixin to baseconst Mixin = ;