Skip to content

nodirt/defineClass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build status

defineClass - simple yet powerful OOP and AOP for JavaScript

Besides classic OOP defineClass has:

  1. Traits from Scala
  2. Decorators from Python
  3. Nested class overriding
  4. Proxy class and trait generation.

Try online

Try defineClass on JsBin.

Quick start

This is a quick start. Detailed explanations are in the wiki.

Defining a class

// define a class
var Device = defineClass({
  // define a default variable name
  hasBattery: false,

  // define a constructor
  constructor: function (ram) {
    this.ram = ram;
  },

  // define a method
  turnOn: function () {
    console.log("Turning on...");
  }
});

Defining a subclass

// define a subclass
var Phone = defineClass({
  // specify the base class
  _super: Device,

  // override a constructor
  constructor: function (ram, number) {
    // you may have code before calling the base constructor
    this.number = number;
    // call the base contructor
    this._super(ram);
  },

  // override a method
  turnOn: function () {
    // call the base method
    this._super();
    console.log("Playing sound...");
  },

  // define a new method
  dial: function (number) {
    console.log("Dialing to " + number);
  }
});

Applying a trait

// define a trait
var TurnOnWithSplash = defineClass.trait({
  // override "turnOn" method
  turnOn: function () {
    // call the base "turnOn"  
    this._super();
    console.log("Showing splash...");
  }
}); 

// apply a trait to a class
var Smartphone = defineClass({
  _super: [Phone, TurnOnWithSplash]
  // other methods
});
var phone = new Smartphone(1000, "120-1010");
phone.turnOn();
// Output:
//   Turning on...
//   Playing sound...
//   Showing splash...

You can use the same trait in different class hierarchies. Read more about traits here.

Decorating

// define a decorator that adds function call logging
var logging = defineClass.decorator(function (func, info) {
  return function() {
    console.log("Entering " + info.name);
    try {
      return func.apply(this, arguments);
    } finally {
      console.log("Exiting " + info.name);
    }
  };
});

// apply the decorator to all methods
var Phone = defineClass({
  _super: Device,
  // apply the decorator to all methods
  $: logging,

  dial: function (number) {
    console.log("Dialing to " + number);
  }
});

var phone = new Phone();
phone.dial("120-0000");
// Output:
//   Entering dial
//   Dialing to 120-0000
//   Exiting dial

Read more about decorators here.

Installation

$ npm install defineClass

About

Simple yet powerful OOP and AOP for JavaScript

Resources

Stars

Watchers

Forks

Packages

No packages published