
Product
Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
mixin-prototypes
Advanced tools
mixin-prototypes
Yet another suicidal module to implement multiple inheritance with JavaScript.
class TObject {
hello_world() {
console.error('hello, world!');
}
}
class IFoo {
foo() {
console.error('FOO!');
}
}
class IBar {
bar() {
console.error('BAR!');
}
}
const TFooBar = mixin( 'TFooBar', TObject, IFoo, IBar );
const foo_bar = new TFooBar();
foo_bar.hello_world(); // hello, world!
foo_bar.foo(); // FOO!;
foo_bar.foo(); // BAR!;
The object which is retuned by mixin()
function has
source
property. This property is the source code to generate the class which
has just created by the function.
// ...
const TFooBar = mixin( 'TFooBar', TObject, IFoo, IBar );
console.log( TFooBar.source );
> class TFooBar extends TObject {
>
> constructor(...args) {
> super(...args);
> this.ctor(...args);
> }
>
> ctor(...args) {
> }
>
> }
>
> Object.defineProperties( TFooBar.prototype, {
>
> foo:{
> value : IFoo.prototype.foo,
> enumerable : false,
> configurable : true,
> writable : true,
> },
>
> bar:{
> value : IBar.prototype.bar,
> enumerable : false,
> configurable : true,
> writable : true,
> },
>
> });
You can define a method which name is ctor()
on each class which are to be
inherited. The generated class will call every ctor()
methods on its parent classes
whenever the class is instantiated.
class TObject {
}
class IFoo {
foo() {
console.error( `${this.foo_name}!` );
}
ctor() {
this.foo_name = 'FOOO';
}
}
class IBar {
bar() {
console.error( `${this.bar_name}!` );
}
ctor() {
this.bar_name = 'BAAR';
}
}
const TFooBar = mixin( 'TFooBar', TObject, IFoo, IBar );
console.log( TFooBar.source );
const foo_bar = new TFooBar();
foo_bar.foo(); // FOOO!;
foo_bar.bar(); // BAAR!;
console.log( foo_bar.source );
> class TFooBar extends TObject {
>
> constructor(...args) {
> super(...args);
> this.ctor(...args);
> }
>
> ctor(...args) {
> IFoo.prototype.ctor.apply( this, args );
> IBar.prototype.ctor.apply( this, args );
> }
>
> }
>
> Object.defineProperties( TFooBar.prototype, {
>
> foo:{
> value : IFoo.prototype.foo,
> enumerable : false,
> configurable : true,
> writable : true,
> },
>
> bar:{
> value : IBar.prototype.bar,
> enumerable : false,
> configurable : true,
> writable : true,
> },
>
> });
The value which is passed to the constructor of the generated class are passed
to the ctor()
methods. You may want to pass named arguments to the
constructor to avoid name conflicts because the same arguments are passed
multiple times to each of the ctor()
method.
class TObject {
}
class IFoo {
foo() {
console.error(`${this.foo_name}!`);
}
ctor(nargs) {
this.foo_name = nargs.foo;
}
}
class IBar {
bar() {
console.error(`${this.bar_name}!`);
}
ctor(nargs) {
this.bar_name = nargs.bar;
}
}
const TFooBar = mixin( 'TFooBar', TObject, IFoo, IBar );
console.log( TFooBar.source );
const foo_bar = new TFooBar({foo:'FOOOO', bar:'BAAAR' });
foo_bar.foo(); // FOOOO!;
foo_bar.bar(); // BAAAR!;
Please not that the ctor()
method of the direct parent will be implicitly
overriden and not executed.
class TObject {
// what if define ctor() on the direct parent class;
ctor(){
console.error('hello!');
}
}
class IFoo {
foo() {
console.error(`${this.foo_name}!`);
}
ctor() {
this.foo_name = 'FOOO';
}
}
class IBar {
bar() {
console.error(`${this.bar_name}!`);
}
ctor() {
this.bar_name = 'BAAR';
}
}
const TFooBar = mixin( 'TFooBar', TObject, IFoo, IBar );
console.log( TFooBar.source );
// TObject.ctor is overriden by the system and not executed.
const foo_bar = new TFooBar();
foo_bar.foo(); // FOOO!;
foo_bar.bar(); // BAAR!;
multiple-inheritance.js
to mixi-prototypes
and updated to v1.0.1
.inheritMultipleClasses()
to mixin()
.Thank you for your attention.
FAQs
Yet another JavaScript module to implement multiple inheritance
We found that mixin-prototypes 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 Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.