Skip to content
a utility for creating expressive classes in JavaScript
JavaScript
Pull request Compare This branch is 5 commits ahead, 15 commits behind ded:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
build
make
src
test
.gitmodules
Makefile
README.md
klass.js
klass.min.js
package.json

README.md

Klass

klass is an expressive, cross-platform JavaScript class implementation with a slick, classical interface to prototypal inheritance.

Downloads

You can download the latest version of klass from GitHub, or install it using the Node Package Manager:

$ npm install -g klass

Usage

Creating a Class

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

var fat = new Person('Jacob');

fat.name; // => Jacob
fat.walk(); // => Jacob: Walking...
fat.run(); // => Jacob: Running...

Subclassing

var SuperHuman = Person.extend(function (name) {
  // Superclass is automatically invoked...
}).methods({
  walk: function () {
    // Note: `this.callSuper(arguments)` is equivalent to `arguments.callee.supr.call(this, ...)`.
    return this.callSuper(arguments).toUpperCase();
  },
  run: function () {
    // Note: `arguments.callee.supr.call(this, ...)` is equivalent to `this.callSuper(arguments)`.
    return arguments.callee.supr.call(this).toLowerCase();
  }
  fly: function () {
    return this.name.toUpperCase() + ': Flying...';
  }
});

var zelda = new SuperHuman('Zelda');

zelda.name; // => Zelda
zelda.walk(); // => ZELDA: WALKING...
zelda.run(); // => zelda: running...
zelda.fly(); // => ZELDA: Flying...

Object Literals

In addition to providing a constructor function as the first argument to klass, you can also pass an object literal containing your class methods. If you specify an initialize method, it will be automatically called upon instantiation.

var Foo = klass({
  foo: 0,
  initialize: function () {
    this.foo = 1;
  },
  getFoo: function () {
    return this.foo;
  },
  setFoo: function (value) {
    this.foo = value;
    return this.getFoo();
  }
});

var foo = new Foo;

foo.foo;
// => 1
foo.getFoo();
// => 1
foo.setFoo(2);
// => 2

Implementing

You can use the extend and implement methods to override or mix-in instance methods.

var Alien = SuperHuman.extend({
  beam: function () {
    // Beam into space...
  }
});
Alien.Beam = 'Up';
// ...
var spazoid = new Alien('Zoopo');
// ...
if (Alien.Beam == 'Down') {
  spazoid.implement({
    beam: function () {
      this.callSuper(arguments);
      // Beam is down; use jets...
    }
  });
}

Supported Environments

klass has been tested with the following web browsers, CommonJS environments, and JavaScript engines.

Web Browsers

Example

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Example Class</title>
  </head>
  <body>
    <script src="/path/to/klass.js"></script>
    <script>
      var Foo = klass({
        // ...
      });
      var Bar = Foo.extend({
        // ...
      });
      Bar.implement({
        // ...
      });
    </script>
  </body>
</html>

CommonJS Environments

Example

var klass = require('klass').klass;
var Foo = klass({
  // ...
});
var Bar = Foo.extend({
  // ...
});
Bar.implement({
  // ...
});

JavaScript Engines

Example

load('/path/to/klass.js');
var Foo = klass({
  // ...
});
var Bar = Foo.extend({
  // ...
});
Bar.implement({
  // ...
});

Contributing

Check out a working copy of the klass source code with Git:

$ git clone git://github.com/ded/klass.git

If you'd like to contribute a feature or bug fix, you can fork klass, commit your changes, and send a pull request. Please make sure to run make tests and make build before submitting your request.

Alternatively, you may use the GitHub issue tracker to submit bug reports and feature requests. For the former, please make sure that you detail how to reproduce the bug, including the environments that exhibit it.

Contributors

Follow our software on Twitter: @dedfat.

Something went wrong with that request. Please try again.