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

createClass

v1.0.7

Published

createClass inspired by React

Downloads

3,256

Readme

createClass NPM version downloads

build

docs - install

Just sugar.

usage

Contrived example time! :D

var createClass = require('createClass');

var Logger = createClass({
  create: function Logger (props) {
    console.log(this.constructor.name, 'created with props', props);

    Object.keys(this.constructor.prototype).forEach(function (method) {
      var self = this;
      self[method] = function (/* arguments */) {
        console.log(self.constructor.name, 'is going to', method);
        self.constructor.prototype[method].apply(self, arguments);
      };
    }, this);
  }
});

var Animal = createClass(Logger, {
  create: function Animal (props) {
    Animal.super_.call(this, props);
    this.props = props;
  }
});

var FelineMixin = {
  talk: function () {
    console.log('meoww (%s)', this.props.thinking);
  }
};

var Cat = Animal.createClass({
  mixins: [FelineMixin],
  create: function Cat (props) {
    Cat.super_.call(this, props);
  }
});

var cat = new Cat({ thinking: 'hungry' });

setInterval(function () {
  cat.talk();
}, 100);

docs

The module.exports a function

var createClass = require('createClass');

When assigning createClass to a function makes it the super class.

function Constructor () {}
Constructor.createClass = createClass;

var Child = Constructor.createClass();
// behold! Child will inherit from `Constructor` now

createClass

Can take 2 arguments

function createClass ([Function Super, Object spec])

arguments

  • Super, type function, to use as super class
  • spec, type object, to be added to the constructor prototype. Can be either passed as 1st or 2nd argument to the function

returns a new class with the given spec in its prototype and two static methods

  • NewClass.create: creates a new instance (up to 3 arguments)
  • NewClass.createClass: same as createClass but using NewClass as super

example:

var Animal = createClass({
  talk: function () {
    console.log('?');
  }
});

var Cat = createClass(Animal, {
  talk: function () {
    console.log('meoww');
  }
});

var cat = Cat.create();

cat.talk();
// => meoww

spec has some special properties

spec.create

Should be a Function.

If given, it will be used the constructor function for the returned class.

The module is using inherits to do inheritance which means: super_ is set as a static property.

var Animal = createClass({
  create: function Animal (props) {
    this.props = props;
  }
});

and since Super can be passed as first argument

var Cat = createClass(Animal, {
  create: function Cat (/* arguments */) {
    Cat.super_.apply(this, arguments);
  }
});

var cat = Cat.create({ thinking: 'hungry' });

console.log(cat.props);
// => { thinking: 'hungry' }

spec.mixins

Same as merge/extend/Object.assign with the resulting class prototype. The element at the tail of the array is the first to be used and from there back to the beginning of the array.

Each element of the Array can be object or function.

When an element is ...

  • object, its properties are directly assigned
  • function, its prototype (enumerable) properties are assigned

In both cases there is no method override. If was defined on already it will not be overridden.

function Animal () {}
Animal.prototype.talk = function () {
  console.log('Yo!');
};

function Mammal () {}
Mammal.prototype.hasFur = function () {
  return true;
};

var AnimalMixin = {
  poop: function () {
    console.log('Pooping now. Wait for it...');
    setTimeout(function () {
      console.log('.');
    }, Math.random()*1000);
  }
};

var Bear = createClass(Animal, {
  mixins: [AnimalMixin, Mammal]
  poop: function () {
    console.log('Bear pooping now!');
    console.log('.');
    console.log('Bear pooped!');
  },
  talk: function () {
    console.log('bearrrrrrr!');
  }
});

var Cat = createClass(Animal, {
  mixins: [AnimalMixin, Mammal]
  talk: function () {
    console.log('meowww');
  }
});

Mixins are good looking because you can put them together with just objects, and one may think there is less of the "you wanted a bannana but you got a gorilla holding a banana" problem, but there are still issues with them see: mixins are considered harmful.

So we should be careful grasshoppers.

spec.statics

Object of static properties to add to the new class.

var Animal = createClass({
  statics: {
    isMammal: function (animal) {
      if (animal && typeof animal !== 'function') {
        return animal.props && animal.props.neocortex;
      }
    }
  }
});

install

With npm

npm install --save-dev createClass

license

The MIT License (MIT)

Copyright (c) 2016-present Javier Carrillo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.