npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mixin-prototypes

v1.0.5

Published

Yet another JavaScript module to implement multiple inheritance

Downloads

3

Readme

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!;

Principle

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,
>      },
>
>    });

Initializers

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!;

History

  • Released v1.0.0 (Wed, 30 Nov 2022 19:50:50 +0900)
  • Released v1.0.1 (Tue, 03 Jan 2023 11:38:21 +0900) renamed multiple-inheritance.js to mixi-prototypes and updated to v1.0.1.
  • Released v1.0.2 (Tue, 03 Jan 2023 14:01:53 +0900) renamed inheritMultipleClasses() to mixin().
  • Released v1.0.3 (Tue, 03 Jan 2023 15:57:28 +0900) supported inheritting static methods.
  • Released v1.0.4 (Wed, 04 Jan 2023 11:46:38 +0900) removed unnecessary log output.
  • Released v1.0.5 (Wed, 04 Jan 2023 11:50:09 +0900) this time, it has been rebuilt.

Conclusion

Thank you for your attention.