javascript-class

1.0.2 • Public • Published

jClass

A small but powerful javascript Class system implementation

Function

To provide a javascript Class combined with single-inheritance and multiple-mixins.

Core API

Declare:

jClass.declare(SuperClass/**<Function>*/, mixins/**<Array.<plain object>>*/, props/**<plain object>*/)

Mixin:

jClass.mixin(target/**<object>*/, mixins/**<Array>*/);

Keywords:

  • this.$super Call super function inside overridden func
  • $constructor Prop key which define constructor function (which is called on creating new class instance)

Usage

Declare a Base class

var BaseClass = jClass.declare(null, null, {
        $constructor: function () {
            console.log('Base constructor');
        },
        propA: 'valueA',
        func: function () {
            console.log('Base func');
        }
    });

Define mixin (which is just a plain object) and call super func

var mx = { func: function (argv) {
        this.$super();
        console.log('mx func with arg value as "' + argv + '"');
    }}

Inherit existed class and mixin

var SubClass = jClass.declare(BaseClass, [ mx ], {
        $constructor: function () {
            this.$super(/**may change any arguments here*/);
            console.log('Sub constructor');
        },
        func: function () {
            this.$super('hello');
            console.log('Sub func');
        }
    };

Instantiation

var subInstance = new SubClass();
// produce console log ==>
//    Base constructor
//    Sub constructor
subInstance.func();
// produce console log ==>
//    Base func
//    mx func with arg value as "hello"
//    Sub func

Package Sidebar

Install

npm i javascript-class

Weekly Downloads

4

Version

1.0.2

License

MIT

Last publish

Collaborators

  • adventure-yunfei