Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add mixins to ES6 javascript classes?

Tags:

In an ES6 class with some instance variables and methods, how can you add a mixin to it? I've given an example below, though I don't know if the syntax for the mixin object is correct.

class Test {   constructor() {     this.var1 = 'var1'   }   method1() {     console.log(this.var1)   }   test() {     this.method2()   } }  var mixin = {   var2: 'var2',   method2: {     console.log(this.var2)   } } 

If I run (new Test()).test(), it will fail because there's no method2 on the class, as it's in the mixin, that's why I need to add the mixin variables and methods to the class.

I see there's a lodash mixin function https://lodash.com/docs/4.17.4#mixin, but I don't know how I could use it with ES6 classes. I'm fine with using lodash for the solution, or even plain JS with no libraries to provide the mixin functionality.

like image 443
user779159 Avatar asked Feb 15 '17 10:02

user779159


People also ask

How use JavaScript mixin?

Mixins can make use of inheritance inside themselves. For instance, here sayHiMixin inherits from sayMixin : let sayMixin = { say(phrase) { alert(phrase); } }; let sayHiMixin = { __proto__: sayMixin, // (or we could use Object. setPrototypeOf to set the prototype here) sayHi() { // call parent method super.

What are classes in ES6?

There are two types of Class in ES6: parent class/super class: The class extended to create new class are know as a parent class or super class. child/sub classes: The class are newly created are known as child or sub class. Sub class inherit all the properties from parent class except constructor.

What is the difference between a mixin and inheritance?

Mixins are sometimes described as being "included" rather than "inherited". In short, the key difference from an inheritance is that mix-ins does NOT need to have a "is-a" relationship like in inheritance. From the implementation point of view, you can think it as an interface with implementations.


1 Answers

Javascript's object/property system is much more dynamic than most languages, so it's very easy to add functionality to an object. As functions are first-class objects, they can be added to an object in exactly the same way. Object.assign is the way to add the properties of one object to another object. (Its behaviour is in many ways comparable to _.mixin.)

Classes in Javascript are only syntactic sugar that makes adding a constructor/prototype pair easy and clear. The functionality hasn't changed from pre-ES6 code.

You can add the property to the prototype:

Object.assign(Test.prototype, mixin); 

You could add it in the constructor to every object created:

constructor() {     this.var1 = 'var1';     Object.assign(this, mixin); } 

You could add it in the constructor based on a condition:

constructor() {     this.var1 = 'var1';     if (someCondition) {         Object.assign(this, mixin);     } } 

Or you could assign it to an object after it is created:

let test = new Test(); Object.assign(test, mixin); 
like image 120
lonesomeday Avatar answered Sep 28 '22 17:09

lonesomeday