chainchainchain


A liberal inheritance JavaScript micro library for object oriented programming. It elegantly avoids the diamond problem and is compatible with any inheritance pattern and other class systems. Take this example:

I want both objects joe and alex to inherit from cop. However, since an object can only inherit from one prototype, I had to create two different cops in order to make joe's parent inherit from human and alex's parent to inherit from robot. But I want the changes I make to cop to be reflected on joe and alex's behaviour. I want them to inherit from the same cop:

Not only do I want both to inherit from the same cop, I want joe chained to cop, human and being while alex is chained to cop, robot and being. I don't want robot to affect joe nor human to affect alex.

This library allows for efficient and safe multiple inheritance, avoiding inefficient property copy or observer type patterns. Taking advantage of metaprogramming and dynamic dispatch, it elegantly allows for independent lineage:

This means I can define the lineage of any object as I like, not interfering with any other objects' lineage. I can have all sorts of forks, loops and paradoxes in the inheritance tree. It's simpler than it looks, consider the following tree:

Take object A. Its chain has been defined as B C I P R. These are the objects in A's chain. But each one of these objects can also have its own chain, branching the chain tree. When getting a property from A, you can choose to look only in its chain or you can choose to look up the entire tree. If you choose the latter you must be aware of precedence, signified here by alphabetical order. As you can see, younger chained objects's whole chains are considered younger than older chained objects.

Mind that in spite of every object being represented with a different letter here, objects can repeat themselves throughout the chain tree. This means that (B===F) can be true, allowing for tridimensional inheritance chains to be designed. All the connections between chained objects are live, wich means that if I make changes to N, A will retrieve N's updated value when required.

While B and C are in A's chain, C doesn't necessarily have to be on B's chain, as every object has its own independent chain and therefore its own tree.

About me

I am a Portuguese web developer, designer and drummer from the city of Porto. If you have any doubts, suggestions or a will to buy me a beer get in touch. Also check out my portfolio.