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

class-factory-js

v1.0.6

Published

An ES6-inspired API for defining classes in ES5.

Downloads

4

Readme

Object.classFactory Build Status

An ES6-inspired API to create "classes" in ES5.

Features

  • Class
  • (Multiple) Inheritance
  • Abstract method/class
  • Getters & setters
  • Static

[Object.]classFactory([parents, ] definition)

Returns a class (i.e. function).

  • parents: [optional] Parent classes (i.e. function) in the form of an array.
  • definition: A callback function used to create the class.
Class Object

The class obejct created by classFactory contains the follows properties:

  • constructor: The class constructor.
  • definition: The function used to define the class.
  • parents: An array of parent class object.
  • abstract: All abstract methods.
  • get: Getters.
  • set: Setters.
  • create(...): The method for creating instances.
Class Instance

Each instance of class contains a .instanceOf(classObject) function, which allows to check if an object is the instance of a class.

Examples

In browser:
var Person = Object.classFactory(function () {
        this.constructor = function (name, age) {
            this.name = name;
            this.age = age;
        };

        this.getName = function () {return this.name;};
        this.getAge = function () {return this.age;};
    });

var guy = Person.create('Someone', 10);
guy.getName(); // Someone
guy.getAge(); // 10

var Coder = Object.classFactory([Person, AnotherClass], function (P) { // P becomes the shorthand of Person
        this.constructor = function (name, age, language) {
            P.call(this, name, age);
            this.language = language;
        };

        this.getLanguage = function () {return this.language;};
    });

var monkey = Coder.create('Code Monkey', 20, 'JavaScript');
monkey.getName(); // Code Monkey
monkey.getAge(); // 20
monkey.getLanguage(); // JavaScript

monkey.instanceOf(Coder); // true
monkey.instanceOf(Person); // true
In Node.js:

You can import classFactory by, for example, var classFactory = require('class-factory-js');. Then var Person = classFactory(...).

Getters & Setters

Getters & setters can be defined through this.get and this.set in the class definition callback function.

For example:

var Person = Object.classFactory(function () {
  this.constructor = function (name, age) {
    this.name = name;
    this.age = age;
  };

  // Define a getter
  this.get.info = function () {
    return this.name + ' -- ' + this.age;
  };

  // Define a setter
  this.set.info = function (val) {
    this.name = val;
  };
});

var p = Person.create('Superhero', 1);
p.info; // 'Superhero -- 1'
p.info = 'Superman';
p.name; // 'Superman'

Static

static is supported by using this.static.

For example:

var Person = Object.classFactory(function () {
  this.static.type = 'Human';
  this.static.sayHi = function () {
    return 'Hi';
  }
});

Person.type; // 'Human'
Person.sayHi(); // 'Hi'

Abstract methods

The below example shows how to define and implement abstract method. All abstract methods should be defined under this.abstract property.

var Person = Object.classFactory(function () {
  this.constructor = function (name, age) {
    this.name = name;
    this.age = age;
  };

  this.abstract.getName = function () {};
  this.abstract.setName = function (name) {};
});

Person.create() // TypeError: Person is an abstract class.

var Boy = Object.classFactory([Person], function (P) {
  this.constructor = function (name, age) {
    P.call(this, name, age);
  };

  this.getName = function () {
    return this.name;
  };

  this.setName = function (name) {
    this.name = name;
    return this;
  };

  this.toString = function () {
    return this.name + ' is now ' + this.age + '-year old.';
  };
});

var boy = Boy.create('Pretty Boy', 1);

Notice that a TypeError will be thrown if not all the abstract methods are implemented.

License

MIT