Skip to content

Function: mixin()

ts
function mixin(...mixins): any;

Creates a new class by combining multiple classes (mixin pattern). 通过组合多个类创建新类(混入模式)。

Parameters

ParameterTypeDescription
...mixinsany[]The classes to combine / 要组合的类

Returns

any

A new class with combined functionality / 具有组合功能的新类

Example

typescript
// Define base classes
class Walkable {
  walk() {
    console.log('Walking...')
  }
}

class Swimmable {
  swim() {
    console.log('Swimming...')
  }
}

class Flyable {
  fly() {
    console.log('Flying...')
  }
}

// Create combined classes
const Duck = mixin(Walkable, Swimmable, Flyable)
const Fish = mixin(Swimmable)
const Bird = mixin(Walkable, Flyable)

// Use the mixed classes
const duck = new Duck()
duck.walk() // => 'Walking...'
duck.swim() // => 'Swimming...'
duck.fly() // => 'Flying...' *
const fish = new Fish()
fish.swim() // => 'Swimming...'
// fish.walk() // Error: walk is not a function

// With properties and constructor logic
class HasName {
  constructor() {
    this.name = 'Unknown'
  }
  getName() {
    return this.name
  }
}

class HasAge {
  constructor() {
    this.age = 0
  }
  getAge() {
    return this.age
  }
}

const Person = mixin(HasName, HasAge)
const person = new Person()
person.getName() // => 'Unknown'
person.getAge() // => 0

Since

1.0.0

Released under the MIT License.