classx

0.2.0 • Public • Published

ClassX

travis-ci.org

Basic module that provide Backbone like extend with _super functionality. Also mixins.

  • extend - you can extend any of your class
  • _super - easy invoke parent's methods
  • mixins - mix any object to your class

Usage

Extend

/**
 * @class Note
 * @extends ClassX
 */
var Note = ClassX.extend({
    /**
     * @private
     */
    _defineProperties: function () {
        this._super();
 
        /**
         * @type {Date} 
         * @private
         */
        this._dateCreation = this._options.dateCreation || new Date();
    },
 
    /**
     * @constructor
     * @protected
     */
    _initialize: function() {
        this._super();
 
        console.log('This note was created ' + this._dateCreation.getTime());
    }
});
 
/**
 * @class PrivateNote
 * @extends Note
 */
var PrivateNote = Note.extend({
 
    /**
     * @constructor
     * @protected
     */
    _initialize: function() {
        this._super();
 
        console.log('No one can see this.');
    }
});
 
var note = new Note({dateCreation: new Date(1991)}); // This note was created ...
var secretNote = new PrivateNote(); // This note was created ... No one can see this.

Mixins

Mixins are common objects that you can mix into any of your class.

/**
 * @mixin WithEncryption
 */
var WithEncryption = {
    encrypt: function () {
        console.log('encrypted!');
    }
}
 
/**
 * @class ClassWithEncryption
 * @mixes WithEncryption
 */
var ClassWithEncryption = ClassX.mix(WithEncryption).extend({
    /**
     * @constructor
     * @protected
     */
    _initialize: function() {
        this._super();
 
        this.encrypt();
    }
});
 
var instance = new ClassWithEncryption(); // encrypted!
 

Install

npm install objectx

Run tests

mocha --reporter spec

Package Sidebar

Install

npm i classx

Weekly Downloads

5

Version

0.2.0

License

GPL-2.0

Last publish

Collaborators

  • syooo