https://github.com/creativelive/classify-js
Factory for creating constructor functions which inherit from other functions -- classical-style.
https://github.com/creativelive/classify-js
Last synced: 2 months ago
JSON representation
Factory for creating constructor functions which inherit from other functions -- classical-style.
- Host: GitHub
- URL: https://github.com/creativelive/classify-js
- Owner: creativelive
- Created: 2014-05-30T16:20:29.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-06-18T18:15:06.000Z (about 12 years ago)
- Last Synced: 2025-09-28T13:14:11.942Z (9 months ago)
- Language: JavaScript
- Size: 237 KB
- Stars: 0
- Watchers: 11
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
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.
```