0.10.7 • Published 10 years ago

classify-js v0.10.7

Weekly downloads
24
License
MIT
Repository
github
Last release
10 years ago

Factory for creating constructor functions which inherit from other functions -- classical-style.

// Example Usage:

var classify = require('classify-js');

var howManyMammals = 0;

var Mammal = classify({
  name : 'Mammal',
  inherits : [],
  initialize : function(options) {
    this._hairColor = options.hairColor;
    Mammal.recordBirth();
  },
  classMethods : {
    getHowManyMammals : function() {
      return howManyMammals;
    },
    recordBirth : function() {
      howManyMammals += 1;
    },
    recordDeath : function() {
      howManyMammals -= 1;
    }
  },
  instanceMethods : {
    getHairColor : function() {
      return this._hairColor;
    }
  }
});

var Human = classify({
  name : 'Human',
  inherits : [Mammal],
  initialize : function(options) {
    this._language = options.language;
  },
  instanceMethods : {
    speak : function() {
      return "I speak " + this._language;
    },
    die : function() {
      Human.recordDeath();
      return "croak";
    }
  }
});

var human = new Human({
  hairColor : 'blonde',
  language : 'english'
});

// The new Human instance inherits the Mammal instance methods, e.g getHairColor().
console.log("The newly-constructed human has hair-color:\n", human.getHairColor());
console.log("The newly-constructed human says:\n", human.speak()); // Human instance method.

// The Human constructor function inherits the Mammal class methods, e.g. getHowManyMammals()
console.log("There are " + Human.getHowManyMammals() + " mammals around.");
console.log("Upon dying, the human says:\n" + human.die()); // Human instance method.
console.log("Now there are " + Human.getHowManyMammals() + " mammals around.");



// See the test directory for an example of how to split classes into separate files.
0.10.7

10 years ago

0.10.6

10 years ago

0.10.5

10 years ago

0.10.4

10 years ago

0.10.3

10 years ago

0.10.2

10 years ago

0.10.1

10 years ago

0.10.0

10 years ago