-
Notifications
You must be signed in to change notification settings - Fork 2
Getting Started
To get started include ds.min.js Creating a class is simple. Just call the class() function and pass it a construct object. For example:
ds.class({
type: 'MyClass', /* The optional type name */
});
The returned class object can new be "newed" Now obviously that class isn't very interesting. Usually you will want to define a constructor and some functions in the class. To create class member variables just use the this * keyword inside of a class function.
var Color = ds.make.class({
type: 'Color',
constructor: function (r,g,b) {
this.r = r; /* now r,g, and b are available to */
this.g = g; /* other methods in the Color class */
this.b = b;
}
});
var red = new Color(255,0,0); // using the new keyword to instantiate the class
A class can contain any functions you want. For example, you may want to add a function that returns the luminance of your color...
var Color = ds.make.class({
type: 'Color',
constructor: function (r,g,b) { /* ... */ },
lum: function(){
return this.r + this.r + this.g + this.g + this.g + this.b * 0.166;
}
});
One of the best ways to create reusable code is to use class inheritance. A class can inherit the methods * from any other classes you create. The is achieved by using the 'inherits' property which accepts a class * object or array of class objects as a parameter:
var a= ds.make.class({
type: 'a',
constructor: function (x) { this.val = x; },
mul: function (s) {
this.val *= s;
return this;
}
});
var b= ds.make.class({
type: 'b',
inherits: a,
constructor: function (x) { this.val = x; },
sub: function (s) {
this.val -= s;
return this;
}
});
var o = new b(5);
var output = o.mul(3).sub(5).val; // output = 10
ds.oop supports single, multiple, multilevel, hybrid, and hierarchical inheritance.
You can also use code contracts using the 'implements' property...
var IThing= {
dothing: function(thing) {} /* this is an interface signature */
};
var Thing = ds.make.class({
type: 'Thing',
implements: IThing,
constructor: function(){},
dothing: function(thing){ /* enforced by contract IThing */
console.log('I am doing ' + thing);
}
};
ds.oop will throw an error to let you know if your class does not implement all of the signatures in the contract. It will even tell you exactly which signatures are missing. Multiple inheritance and multiple interfaces are allowed on any class. As a general rule whenever there is an conflict with multiple inheritance or multiple interfaces, ds.oop gives priority to the left most class in the list.
var Thing = ds.make.class({
type: 'Thing',
inherits: [Class1,Class2,Class3],
implements: [Interface1,Interface2,Interface3],
constructor: function(){},
// ...
};
Hopefully you are starting to see how ds.oop can help you write javascript code smarter and faster. For some more examples see classtest.js in the /test folder.
##Notes
- ds.oop does not have a $super object like prototype.js. (Since multiple-inheritance is allowed how would $super know what to point at?) therefore if you override a base method the only way to access it is thorough the prototype like this: base.prototype.method.call(this, arg1, arg2, argN...);
- ds.oop does not support constructor inheritance. If you have common functionality you should put it into a method.
If you like this project feel free to send me an email:
digital.synapse.software [ಠ_ಠ] gmail.com