Klass
a utility for creating expressive classes in JavaScript
Install / Use
/learn @ded/KlassREADME
Klass
An expressive, cross platform JavaScript Class provider with a classical interface to prototypal inheritance.
API
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()
Object Literal Interface
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
// 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 compliant and provides the Modules 1.1 interface to allow two flavors of development. See the implementations below:
Browser
<script src="path/to/klass.js"></script>
<!-- klass() is exposed to context -->
As a node module
npm install klass
var klass = require('klass')
Ender compatibility
add klass to your ender compilation
ender add klass
Developers
npm install
make
make test
Keep your edits localized to src/klass.js
LICENSE
MIT
Happy Klassing!
Related Skills
node-connect
345.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
106.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
