![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
mixwith
Advanced tools
Readme
A simple, powerful and safe mixin library for ES6.
mixwith
differs from other mixin approaches because it does not copy properties from one object to another. Instead, mixwith
works with "subclass factories" which create a new class that extends a superclass with the mixin - this is called a mixin application.
Subclass factory style mixins take advantage of two awesome features of ES6 classes: class expressions, and expressions in the extends
clause of a class declaration.
let MyMixin = (superclass) => class extends superclass {
// mixin methods here
};
class MyClass extends MyMixin(MySuperClass) {
// class methods here, go ahead, use super!
}
class MyClass extends mix(MySuperClass).with(MyMixin, OtherMixin) {
// class methods here, go ahead, use super!
}
mixwith
preserves the object-oriented inheritance properties that classes provide, like method overriding and super
calls, while letting you compose classes out of mixins without being constrained to a single inheritance hierarchy, and without monkey-patching or copying.
Methods in subclasses can naturally override methods in the mixin or superclass, and mixins override methods in the superclass. This means that precedence is preserved - the order is: subclass -> mixin__1 -> ... -> mixin__N -> superclass.
super
worksSubclasses and mixins can use super
normally, as defined in standard Javascript, and without needing the mixin library to do special chaining of functions.
Since super()
works, mixins can define constructors. Combined with ES6 rest arguments and the spread operator, mixins can have generic constructors that work with any super constructor by passing along all arguments.
Typical JavaScript mixins usually used to either mutate each instance as created, which can be bad for performance and maintainability, or modify a prototype, which means every object inheriting from that prototype gets the mixin. Subclass factories don't mutate objects, they define new classes to subclass, leaving the original superclass intact.
A mixin is simply a function that takes a superclass and returns a subclass of it, using ES6 class expressions:
let MyMixin = (superclass) => class extends superclass {
constructor(args...) {
// mixins should either 1) not define a constructor, 2) require a specific
// constructor signature, or 3) pass along all arguments.
super(...args);
}
foo() {
console.log('foo from MyMixin');
// this will call superclass.foo()
super.foo();
}
};
Mixins defined this way do not require any helpers to define or use. You can use this pattern without mixwith
at all!
Without mixwith
, just invoke them inside a classes extends
clause:
class MyClass extends MyMixin(MySuperClass) {
}
mixwith
provides a helper that's a bit nicer when applying multiple mixins, and adds some features like mixin-deduplication and @@hasInstance
support (@@hasInstance
overloads instanceof
, but isn't supported in any browsers yet).
class MyClass extends mix(MySuperClass).with(MyMixin) {
}
Classes that use mixins can define and override constructors and methods as usual.
class MyClass extends mix(MySuperClass).with(MyMixin) {
constructor(a, b) {
super(a, b); // calls MyMixin(a, b)
}
foo() {
console.log('foo from MyClass');
super.foo(); // calls MyMixin.foo()
}
}
FAQs
A simple, powerful mixin applier for JavaScript classes
The npm package mixwith receives a total of 100,178 weekly downloads. As such, mixwith popularity was classified as popular.
We found that mixwith demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.