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

lang-def

v0.2.0

Published

Lightweight utility module for creating javascript classes.

Downloads

1

Readme

#lang-def status

Lightweight utility module for creating javascript classes.

standards

copyright xkcd

Ok, Javascript already uses OOP, but based in prototypes. It is not bad, but occasionally the class system is the best approach. This library help you to create "raw vanilla" Javascript classes, for the best integrating with another codes and libraries with the best performance.

#Features

  • Great performance.
  • Works in node.js and browsers
    • In node.js use the native inherits.
    • Use ES5 if is available, or the legacy way in legacy environment (cough cough IE<9).
  • Support to classic inhiterance.
  • Support to mixins.
  • Support to CommonJs (like node.js), AMD (like requireJs) and as vanilla global module.
  • Secure. If you forget to use the "new" operator to instantiate then TypeException is fired, so not fail silently.
  • Elegant API & clean code base.

#Quick Example

var def = require('def')

var Point = def({
	new: function (x, y) {
		this.x = x;
		this.y = y;
	},
	distance: function (p) {
		return Math.sqrt(
			Math.pow(p.x - this.x, 2) +
			Math.pow(p.y - this.y, 2)
		)
	}
})

var a = new Point(2, 4)
var b = new Point(5, -6)

a.distance(b);
// -> 10.44030650891055

jsfiddle.net/d0y6jLv5/

#Installation npm:

$ npm install lang-def

bower:

$ bower install lang-def

Or simply download.

##Import

###In CommonJS (like node.js)

var def = require('lang-def')

###In AMD (like requireJS)

require.config({
    paths: {
        def: 'libs/lang-def/def'
    }
});

Then simply use

require(['def'], function (def) {
	// use def
});

###In vanilla (like simple web page)

<script src="my_path/lang-def/def.js"></script>
<script>
	// use window.def or simply def
</script>

#API ##def Creates a constructor function (Class).

Signature:

def([ string name ], [ function BaseClass ], [ array mixins ], [ object props ])
 -> Function
  • optional string name:
    • The name of the class. Useful for debugging.
    • default: undefined, but resolved to 'AnonymousConstructor'.
    • limitations:
      • Must be compilant with the general rules for Javascript variables, i.e. 'Person', 'SuperPerson'. Not 'persons.Person' or 2Person.
      • Is compatible with Chrome Extensions, but not with Chrome Apps, so for developing Chome Apps omit the name. This restriction is due to the use of new Function(...) to generate the name in the runtime.
  • optional function BaseClass:
    • The parent constructor in the classic inhiterance.
    • default: undefined, by definition inherits from Object.
    • limitations:
      • Can be a constructor function (Class) created by def or by vanilla javascript.
      • Currently no work well for DOM Interfaces, like HTMLDivElement.
  • optional array mixins:
    • An array of constructor functions that is mixed from left to right (the right overrides the left).
    • default: [ ]
    • limitations:
      • The same of BaseClass.
  • optional object props:
    • The properties or members of the Class to create.
    • default: { }
    • especial members:
      • optional function new: is the constructor.
        • default: function () { def.mixin(this, arguments); }.
      • (in each method) this.fn(function Class [, string methodName]):
        • return the function methodName of Class in the this clausure.
    • limitations:
      • Must not contain one of the follows special members: mixins_, super_ and fn.

#Examples ##A little Game Egine

The vegetarian game

See in jsfiddle.net/mnrtag0v/8.

##Classic examples ###Inheritance


var Animal = def({
	name: null,
	speak: function () { return 'I am ' + this.name; }
});

var Cat = def(Animal, {
	speak: function() {
		return this.fn(Animal, 'speak') + ', the Cat';
	}
});

var Dog = def(Animal, {
	speak: function() {
		return this.fn(Animall, 'speak') + ', the Dog';
	}
});

var cat = new Cat({ name: 'Canela' });
var dog = new Dog({ name: 'Doky' });

cat instanceof Cat;
// -> true
cat instanceof Animal;
// -> true

dog instanceof Dog;
// -> true
dog instanceof Animal;
// -> true

cat.speak();
// -> 'I am Canela, the Cat'

jsfiddle.net/spL1fvzc/1

###Mixins Example inspired from dgrid

var Widget = def({ ... });
var Grid = def(Widget, { ... });

var Sortable = def({ ... });
var Selectable = def({ ... });
var Resizable = def({ ... });

// Then we need a sortable grid
var MySortableGrid = def(Grid, [Sortable]);

// Then we need a sortable and selectable grid
var MySortableAndResizable = def(Grid, [Sortable, Selectable]);

// one grid
var grid = new MySortableAndResizable({ ... });

grid instanceof Grid; // -> true
grid instanceof Widget; // -> true
grid instanceof Sortable; // -> false
grid instanceof Selectable; // -> false

def.instanceOf(grid, Grid); // -> true
def.instanceOf(grid, Widget); // -> true
def.instanceOf(grid, Sortable); // -> true, because is Mixin
def.instanceOf(grid, Selectable); // -> true, because is Mixin

def.mixinOf(grid, Grid); // -> false
def.mixinOf(grid, Widget); // -> false
def.mixinOf(grid, Sortable); // -> true
def.mixinOf(grid, Selectable); // -> true

##def.mixin Mixin arg0 with arg1, then with arg2 (if exits), etc.

Signature:

def.mixin(object arg0 [, object arg1 [, ... ]]) -> object
  • object arg0
  • ...

The arg0 object is mutated and returned.

##def.mixinOf Check if instance is mixin of Mixin.

Signature:

def.mixinOf(object instance, function Mixin) -> bool
  • object instance
  • function Mixin

##def.instanceOf Check if instance is instance or mixin of MixinOrClass.

Signature:

def.instanceOf(object instance, function MixinOrClass) -> bool
  • object instance
    • the object to check
    • limitations: none
  • function MixinOrClass
    • the constructor function
    • limitations: none, can be a vanilla function.

#Best Practies & Tips

  • Feel free to use def with vanilla constructor functions and in library proyects. def just construct a vanilla constructor, so is not intrusive.
  • In CommonJS or AMD environment use one file per Class.
  • Use the conventional prefix _ for private members. Example _lastPosition: 5. We can over engineering this lib for have a real private concept, but then we like some way for made reflection... so, KISS is the better.
  • To define static properties add them to the constructor. i.e. XHR = def(...); XHR.TYPE_OK = 1; XHR.TYPE_ERR = 0.

#TODO

  • [x] Experimenting................100%
  • [x] Writing the code.............100%
  • [x] Anecdotally Testing..........100%
  • [ ] Automated Testing on Travis..75%
  • [x] Documenting the API..........90%

#LICENSE MIT LICENSE