JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 30
  • Score
    100M100P100Q40384F
  • License MIT

Less than 256 byte JavaScript classical inheritance pattern library

Package Exports

  • class-256.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (class-256.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

class-256.js

npm version Bower version devDependency Status Travis

class-256.js is a less than 256 byte JavaScript classical inheritance pattern library (249 bytes minified or 391 bytes minified with UMD pattern).
Originally based on augment and extend.

Features

  • OOP style declaration
  • Constructor methods (optional - if you don't provide one, the parent's constructor will be called upon instantiation)
  • Working instanceof
  • Public and private/privileged properties/methods
  • 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 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;
    }
});

var helloWorld = new HelloWorld()
helloWorld.say() // 'Hello World!'
helloWorld instanceof HelloWorld // true

var hiWorld = new HelloWorld('Hi ')
hiWorld.say() // 'Hi World!'
hiWorld instanceof HelloWorld // true

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);
emptyExample.isValid() // false

var nameExample = new Extended('John', null);
nameExample.isValid() // false

var addressExample = new Extended(null, 'London');
addressExample.isValid() // false

var validExample = new Extended('John', 'London');
validExample.isValid() // true

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" charset="utf-8"></script>

Files

UMD Minified File
dist/class.umd.min.js
dist/class.min.js
dist/class.umd.js
dist/class.js

Develop

Set up

npm install

Build

Running the following command will start Gulp, which will run ESLint, create the UMD version, run Mocha/Chai tests on node and on PhantomJS with Karma and run UglifyJS:

gulp

License

Copyright © 2015-2016 Horváth Kornél

Licensed under the MIT License.