2.0.1 • Published 5 years ago
class-256.js v2.0.1
class-256.js
class-256.js is a less than 256 byte JavaScript classical inheritance pattern library (221 bytes minified or 363 bytes minified with UMD pattern).
Originally based on augment and extend (repository no longer exists).
Features
- OOP style declaration
- Constructor methods (optional - if you don't provide one, the parent's constructor will be called upon instantiation)
- Working instanceof
- Supports public and private/privileged methods, public and private static properties
- Non-overridden public properties/methods defined in parent naturally accessible
- Overridden public methods defined in parent accessible through parent parameter
Basic usage
var HelloWorld = Class.extend(function() { // default name of .extend() can be changed via constant
this.greeting = 'Hello '; // public property
var world = 'World!'; // private static property
this.constructor = function(greeting) { // default name of .constructor() can be changed via constant
if (typeof greeting != 'undefined') {
this.greeting = greeting;
}
};
this.say = function() { // public method
return privileged.call(this);
};
function privileged() { // private/privileged method
return this.greeting + world;
}
this.setWorld = function(newWorld) {
world = newWorld;
};
this.getWorld = function() {
return world;
};
});
var helloWorld = new HelloWorld();
assert.equal(helloWorld.say(), 'Hello World!');
assert.instanceOf(helloWorld, HelloWorld);
var hiWorld = new HelloWorld('Hi ');
assert.equal(hiWorld.say(), 'Hi World!');
assert.instanceOf(hiWorld, HelloWorld);
helloWorld.setWorld('Earth!');
assert.equal(helloWorld.say(), 'Hello Earth!');
assert.equal(hiWorld.say(), 'Hi Earth!'); // Because 'world' is a static property
Extending
function isNonEmptyString(value) {
return typeof value == 'string' && value.length > 0;
}
var Base = Class.extend(function() {
this.name = '';
this.constructor = function(name) {
this.name = name;
};
// check name
this.isValid = function() {
return isNonEmptyString(this.name);
};
});
var Extended = Base.extend(function(parent) {
this.address = '';
this.constructor = function(name, address) {
parent.constructor.call(this, name); // call parent constructor
// could be also written as: this.name = name;
this.address = address;
};
// check name and address
this.isValid = function() {
return parent.isValid.call(this) && isNonEmptyString(this.address);
// could be also written as: return isNonEmptyString(this.name) && isNonEmptyString(this.address);
};
});
var emptyExample = new Extended(null, null);
assert.isFalse(emptyExample.isValid());
var nameExample = new Extended('John', null);
assert.isFalse(nameExample.isValid());
var addressExample = new Extended(null, 'London');
assert.isFalse(addressExample.isValid());
var validExample = new Extended('John', 'London');
assert.isTrue(validExample.isValid());
For more examples see test/test.js.
Installation
NPM
npm install class-256.js
Bower
bower install class-256.js
Browser
<script src="https://github.com/koffeine/class-256.js/blob/master/dist/class.umd.min.js"></script>
Files
UMD | Minified | File |
---|---|---|
✓ | ✓ | dist/class.umd.min.js |
✓ | dist/class.umd.js |
Develop
Requirements
Building requires NodeJS version 6.0.0 or later.
Set up
npm install
Build
Running the following command will start Gulp, which will run ESLint, create the UMD version, run Mocha/Chai tests and run UglifyJS:
gulp
License
Copyright © 2015-2018 Kornél Horváth
Licensed under the MIT License.