Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
a utility for creating expressive classes in JavaScript
tree: 69113baf13

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
build
make
src
test
.gitignore
.gitmodules
Makefile
README.md
klass.js
klass.min.js
package.json

README.md

Klass.js

An expressive, cross platform JavaScript Class provider with a slick, classical interface to prototypal inheritance.

Interface

creating a Class...

var Person = klass(function (name) {
  this.name = name;
})
  .statics({
    head: ':)',
    feet: '_|_'
  })
  .methods({
    walk: function () {}
  });

Subclassing...

var SuperHuman = Person.extend(function (name) {
  // super class is automagically called
})
  .methods({
    walk: function() {
      this.supr();
      this.fly();
    },

    fly: function() {}

  });

new SuperHuman('Zelda').walk()

Implementing...

(because sometimes you want to overwrite OR mixin an instance method)

// note you can optionally pass an object literal to extend too ;)
var Alien = SuperHuman.extend({
  beam: function() {
    this.supr();
    // beam into space
  }
});

if (beamIsDown) {
  Alien.implement({
    beam: function() {
      this.supr();
      // fallback to jets
      this.jets();
    }
  });
}

Environments

Klass is Common JS compliant and provides the Modules 1.1 interface to allow two flavors of development. See the implementations below:

browser environment

<script src="path/to/klass.js"></script>
<!-- klass() is exposed to context -->

<script type="text/javascript">
  var Foo = klass(fn1);
  var Bar = Foo.extend(fn2);
  Bar.implement({ ... });
</script>

as a module

// your-application.js
var klass = require('path/to/klass');

var Foo = klass(...);

Running the tests

If you want to see shiny passing tests, run the tests make command

% make tests

Install the Package!

By far the easiest way to get started with klass is to simply install the package and hit the ground running!!

% npm install klass

// in your Node application
var klass = require('klass')

Contributors

Something went wrong with that request. Please try again.