Question: 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.

Answer: 
You should probably look at `Object.assign()`. Gotta look something like this:

```
Object.assign(Test.prototype, mixin);

```

This will make sure all methods and properties from `mixin` will be copied into `Test` constructor's prototype object.
I'm surprised to find that none of the answers mentions what I would consider a mixin in the sense of composition (and in contrast to inheritance), which to me is a function that adds functionality to an object. Here's an example making use of both inheritance and composition:

```js
class Pet { constructor(name) { this.name = name } }
class Cat extends Pet { expression = 'miaow' }
class Dog extends Pet { expression = 'bark' }

class Human { constructor(name, age) { this.name = name; this.age = age; } }
class American extends Human { expression = 'say howdy' }

function canSayHello(...contexts) {
  for (const context of contexts) {
    context.sayHello = function() {
     console.log(`Hello my name is ${this.name} and I ${this.expression}`)
    }
  }
}

canSayHello(Pet.prototype, Human.prototype); // apply the mixin

const garfield = new Cat('garfield');
const pluto = new Dog('pluto');
const joebiden = new American('Joe Biden', 79); 

garfield.sayHello();
pluto.sayHello();
joebiden.sayHello();
```