0.2.2 • Published 9 years ago

subtypejs v0.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

subtype

Simple OOP library. Works fast like native inheritance (see benchmarks). The library does not contain any super or analogs because it contaminates the prototype and a bad influence on performance.

##Install For bower users:

bower install subtype

For node.js users:

npm install subtypejs

Also you can download uncompressed and compressed version from GitHub.

##Usage In a browser:

<script src="path/to/subtype.js"></script>

In an AMD loader:

define(['subtype'], function (Subtype) {

  // code...

});

In node.js:

var Subtype = require('subtypejs');

// code...

And then:

var Human = Subtype.extend({
  constructor: function (name) {
    this.name = name;
  },
  say: function (words) {
    return this.name + ': ' + words;
  }
});

var Actor = Human.extend({
  say: function (words) {
    // explicit call the super method
    return 'actor ' + Human.prototype.say.call(this, words);
  }
});

var human = new Human('Robert');
console.log(human.say('Hi!')); // => "Robert: Hi!"

var actor = new Actor('Jeremy');
console.log(actor.say('Hello!')); // => "actor Jeremy: Hello!"

console.log(
  human instanceof Human &&
  human instanceof Subtype &&
  actor instanceof Actor &&
  actor instanceof Human &&
  actor instanceof Subtype
); // => true

console.log(
  Subtype === Subtype.prototype.constructor &&
  Human === Human.prototype.constructor &&
  Actor === Actor.prototype.constructor
); // => true