Class: Mixin

Mixin

Mixin contains functions for creating mixins - a way to do multiple inheritance in JavaScript. Mixin classes can be defined, and their functionality merged into an instance of another class when it is instantiated.
Source:
Examples

To define a Mixin class

const { Mixin } = require('tenkai')

class YourMixinClass extends Mixin {
	static init(obj, options) {
		// Do your initialisation here, like you would in a constructor.
		// obj is the new object, eqivalent of 'this'.
		// The user class can pass options, etc.
		obj.testValue = 'This is a Mixin Class'
	}

	// Add methods as per a standard class:
	mixinMethod() {
		...
		console.log('mixinMethod: '+this.testValue)
		...
	}
}

// Use the Mixin.export() function to export the class.
module.exports = Mixin.export(YourMixinClass)

To use a Mixin class

const YourMixinClass = require('./YourMixinClass')

class YourMixinUserClass {
	constructor(options = {}) {

		// Call the mixin with this and any options
		YourMixinClass(this, options)
	}

	anotherMethod() {
		// Now you can use the properties and methods on the mixin

		console.log(this.testValue)
		this.mixinMethod()
	}
}

Methods

(static) export(klass)

Use export to export the class from the module as a mixin.
Parameters:
Name Type Description
klass Class The class to export as a mixin
Source:
Example
module.exports = Mixin.export(YourMixinClass)