Wednesday, July 8, 2015

Javascript Mixing Pattern

In traditional object oriented languages we could write inherited classes and reuse functionality. However, in javascript, inheritance comes from chaining prototypes.  But for performance reason, you should not have long prototype chain. Therefore in javascript, for code reuse we can use mixing pattern. Multiple inheritance makes it easy by using mixing pattern.
The concept of mixing is to take an object and mixing the functionality with another object.
First we will define two objects.
Then we will define mixings.
Then we will define mixing function.
Now add mixing to the above objects and check the output.
Most important thing to remember is, you are injecting functionality to your code. Therefore you must document it properly in a large code base.

No comments:

Post a Comment