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

myclass

v1.0.2

Published

Probably the fastest JS class system out there

Downloads

32

Readme

my.class.js

Probably the fastest JS class system out there. 100% no wrappers, same perfs as hand-written pure JS classes.

Forked from https://github.com/jiem/my-class. I just packaged it as an NPM module, but I might bring more changes to it with time.

  • [instantiation perfs] (http://jsperf.com/moo-resig-ender-my)
  • [inheritance perfs - calling super constructor] (http://jsperf.com/moo-resig-ender-my/2)
  • [inheritance perfs - calling super method] (http://jsperf.com/moo-resig-ender-my/3)

See a little [demo] (http://myjs.fr/my-class/example/example.html).

My.js class system is not only a class implementation, it's mostly a class design.

See [how My.js classes achieve better perfs] (http://myjs.fr/my-class/).

Create a class

Assume that classes are created in the namespace myLib.

(function() {

  var Person = my.Class({

    STATIC: {
      AGE_OF_MAJORITY: 18
    },

    constructor: function(name, age) {
      this.name = name;
      this.age = age;
    },

    sayHello: function() {
      console.log('Hello from ' + this.name + '!');
    },

    drinkAlcohol: function() {
      this.age < Person.AGE_OF_MAJORITY ?
        console.log('Too young! Drink milk instead!') :
        console.log('Whiskey or beer?');
    }

  });

  myLib.Person = Person;

})();

var john = new myLib.Person('John', 16);
john.sayHello(); //log "Hello from John!"
john.drinkAlcohol(); //log "Too young! Drink milk instead!"

Extend a class

(function() {

  //Dreamer extends Person
  var Dreamer = my.Class(Person, {

    constructor: function(name, age, dream) {
      Dreamer.Super.call(this, name, age);
      this.dream = dream;
    },

    sayHello: function() {
      superSayHello.call(this);
      console.log('I dream of ' + this.dream + '!');
    },

    wakeUp: function() {
      console.log('Wake up!');
    }

  });

  var superSayHello = Dreamer.Super.prototype.sayHello;

  myLib.Dreamer = Dreamer;

})();

var sylvester = new myLib.Dreamer('Sylvester', 30, 'eating Tweety');
sylvester.sayHello(); //log "Hello from Sylvester! I dream of eating Tweety!"
sylvester.wakeUp(); //log "Wake up!"

Private methods

See the section "Private fields and methods" of [this post] (http://myjs.fr/my-class/).

Add methods to a class

my.extendClass(myLib.Dreamer, {

  touchTheSky: function() {
    console.log('Touching the sky');
  },

  reachTheStars: function() {
    console.log('She is so pretty!');
  }

});

Implement classes

 myLib.ImaginaryTraveler = my.Class({
  travel: function() { console.log('Traveling on a carpet!'); },
  crossOceans: function() { console.log('Saying hi to Moby Dick!'); }
});

(function() {

  //Dreamer extends Person implements ImaginaryTraveler
  var Dreamer = my.Class(Person, ImaginaryTraveler, {

    constructor: function(name, age, dream) {
      Dreamer.Super.call(this, name, age);
      this.dream = dream;
    },

    ...

  });

  myLib.Dreamer = Dreamer;

})();

var aladdin = new Dreamer('Aladdin');
aladdin instanceof Person; //true
aladdin instanceof ImaginaryTraveler; //false
aladdin.travel();
aladdin.wakeUp();
aladdin.sayHello();

Afraid to forget the new operator?

var Person = my.Class({

  //you can now call the constructor with or without new
  constructor: function(name, city) {
    if (!(this instanceof Person))
      return new Person(name, city);
    this.name = name;
    this.city = citye;
  }

});