A simple object model for JavaScript.
Klassified provides a base class object that can be subclassed using its
subclass class method, as in the following example:
var animal = object.subclass(function(that, my) {
my.initialize = function(spec) {
my.super(spec);
my.name = spec.name;
}
that.getName = function() {
return my.name;
};
});
animal.class(function(that) {
that.named = function(name) {
return that({name: name});
};
});
var dog = animal.subclass(function(that, my) {
that.getName = function() {
return 'dog named' + that.super();
};
});that represents the receiver (the instance or the class depending on the
context).
Public methods are attached to the instance (that), while protected methods
are attached to my.
Instances can be created by calling a class function like the following:
var milou = dog({name: 'milou'});
milou.getName(); // => 'milouThe spec literal object is passed to my.initialize, which is called upon
instance creation.
my.super should almost always be called from within initialize.
Klassified has support for super calls using either that.super for public
methods or my.super for protected methods.
object provides the following instance methods and properties:
that.klassreturns the class of the instancemy.subclassResponsibilitymethod throwing an exception because a method should have been overridden.my.initializeinitialization method, takes a literal spec object.
object also provides the following class-side methods and properties:
object.subclassesreturns an array of the direct subclasess of a class.object.allSubclassesmethod that returns all the subclasess of a class.object.subclassmethod used to create a new subclass of a class.
Klassified supports abstract classes using abstractSubclass:
var animal = object.abstractSubclass(function(that, my) {
// [...]
});
var dog = animal.subclass(function(that, my) {
// [...]
});
animal(); // => Error: Cannot instantiate an abstract class
dog(); // => New dog instanceKlassified support singleton classes using singletonSubclass:
var service = object.singletonSubclass(function(that, my) {
// [...]
});
service.instance(); // Return the single instance of the class
service(); // => Error: Cannot create new instances of a singleton class, use `instance` instead.Klassified can generate getters and setters for protected properties on my, as
below:
var animal = object.subclass(function(that, my) {
my.initialize = function(spec) {
my.name = spec.name;
};
my.get('name');
my.set('name');
});
var a = animal();
a.setName('milou');
a.getName(); // => 'milou'Custom getters and setters are also supported:
var dog = animal.subclass(function(that, my) {
my.get('name', function() {
return 'A dog named ' + my.name;
});
my.set('name', function(value) {
my.name = value.toUpperCase();
});
});
var a = animal();
a.setName('milou');
a.getName(); // => 'A dog named MILOU'