Skip to main content

@mixin

Syntax

@mixin
@mixin name

Overview

@mixin marks an object as a mixin — a reusable collection of methods and properties intended to be mixed into other classes or objects. Use @mixes on the receiving class to document that it includes a mixin.

Example

/**
* Adds serialisation methods to a class.
* @mixin
*/
const Serializable = {
/**
* Serialize the object to JSON.
* @returns {string}
*/
toJSON() {
return JSON.stringify(this);
},

/**
* Restore the object from JSON.
* @param {string} json
*/
fromJSON(json) {
Object.assign(this, JSON.parse(json));
},
};

/**
* @mixes Serializable
*/
class User {}
Object.assign(User.prototype, Serializable);

See also

Official reference: jsdoc.app/tags-mixin