Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mixins properly in Javascript

I am organizing a small enterprise application but would like to be as DRY as possible. As a result, I've been looking at mixin libraries.

I came across this library and thought it might be a good option as it allows you to mix in and out at run time. Also, I can just have one base class (BaseView) for instance and just mixin off of that.

Questions

  1. What are some real application examples of useful Mixins? (pls no more abstract examples)
  2. Do I even need to extend classes or can I just use this library to manage all extensions and mixins?
like image 931
imrane Avatar asked Nov 29 '12 18:11

imrane


People also ask

What are mixins used in JavaScript?

As defined in Wikipedia, a mixin is a class containing methods that can be used by other classes without a need to inherit from it. In other words, a mixin provides methods that implement a certain behavior, but we do not use it alone, we use it to add the behavior to other classes.

How does a mixin work?

Mixins are a language concept that allows a programmer to inject some code into a class. Mixin programming is a style of software development, in which units of functionality are created in a class and then mixed in with other classes. A mixin class acts as the parent class, containing the desired functionality.

Is a mixin a function?

Functional mixins are composable functions which mix new properties or behaviors with properties from a given object. Functional mixins don't depend on or require a base factory or constructor: Simply pass any arbitrary object into a mixin, and it will be extended.


1 Answers

A mixin is just a different conceptual idea, of how to organize code and inheritance. You can of course combine it with using classical or prototypal inheritance, but it also works stand-alone, so to speak.

For instance, instead of creating "delegated" object properties/lookups (like prototypal inheritance), we would truly "form" new stand-alone objects, from multiple other objects. This is also called "multiple inheritance" sometimes and that cannot get achieved easily with Javascripts prototypal inheritance alone.

As an example:

var pianist = {
   play: function() {}
};

var programmner: {
   code: function() {}
};

And now we could create another Object, like

var Jim = Object.create( null ); // create a fully self-defining object

extend( Jim, pianist );
extend( Jim, programmer );

and this pseudo extend method could look like (ES5):

function extend( target, source ) {
    Object.getOwnPropertyNames( source ).forEach(function( key ) {
        Object.defineProperty( target, key, Object.getOwnPropertyDescriptor(source, key)) });

    return target
}

I actually didn't answer your questions properly, but I felt like there is no real answer to your question. It is as real as you are going to use it, there is no "application specific" use case really.

like image 118
jAndy Avatar answered Sep 20 '22 23:09

jAndy