https://github.com/ded/klass
a utility for creating expressive classes in JavaScript
https://github.com/ded/klass
Last synced: about 1 year ago
JSON representation
a utility for creating expressive classes in JavaScript
- Host: GitHub
- URL: https://github.com/ded/klass
- Owner: ded
- Created: 2011-03-08T23:59:49.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2015-07-16T03:28:45.000Z (almost 11 years ago)
- Last Synced: 2025-04-12T09:19:52.593Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 297 KB
- Stars: 750
- Watchers: 32
- Forks: 78
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-focusnetworks - klass - A utility for creating expressive classes in JavaScript. [](http://spmjs.io/package/klass) (Class / Runner)
README
## Klass
An expressive, cross platform JavaScript Class provider with a classical interface to prototypal inheritance.
## API
### creating a Class
``` js
var Person = klass(function (name) {
this.name = name
})
.statics({
head: ':)',
feet: '_|_'
})
.methods({
walk: function () {}
})
```
### Subclassing
``` js
var SuperHuman = Person.extend(function (name) {
// super class is automagically called
})
.methods({
walk: function() {
this.supr()
this.fly()
},
fly: function() {}
})
new SuperHuman('Zelda').walk()
```
### Object Literal Interface
``` js
var Foo = klass({
foo: 0,
initialize: function() {
this.foo = 1
},
getFoo: function () {
return this.foo
},
setFoo: function (x) {
this.foo = x
return this.getFoo()
}
})
```
*note: initialize will be called on class invocation*
### Implement
because sometimes you want to overwrite OR mixin an instance method
``` js
// note you can optionally pass an object literal to extend too ;)
var Alien = SuperHuman.extend({
beam: function() {
this.supr()
// beam into space
}
})
var Spazoid = new Alien('Zoopo')
if (beamIsDown) {
Spazoid.implement({
beam: function() {
this.supr()
// fallback to jets
this.jets()
}
})
}
```
## Environments
Klass is [Common JS](http://commonjs.org) compliant and provides the [Modules 1.1](http://wiki.commonjs.org/wiki/Modules/1.1) interface to allow two flavors of development. See the implementations below:
### Browser
``` html
```
### As a node module
``` sh
npm install klass
```
``` js
var klass = require('klass')
```
### Ender compatibility
add `klass` to your [ender](http://enderjs.com) compilation
``` sh
ender add klass
```
### Developers
``` sh
npm install
make
make test
```
Keep your edits localized to `src/klass.js`
## LICENSE
MIT
### Happy Klassing!