A micro library for fluent javascript object properties. Allows you to easily create fluent chained setters and getters for simple properties.
The idea is to not have to define every getter and setter to get fluent code like this:
obj.prop1("someValue").prop2("someOtherValue").doSomething();
Just declare that obj has properties prop1 and prop2 and its all handled for you.
Fluent is initialized with an object. Then properties can be added one at a time.
function Circle(){}
Fluent.object(Circle)
.addProperty(x)
.addProperty(y)
.addProperty(r);
Fluent can also take an array of properties.
function Circle(){}
Fluent.object(Circle)
.addProperties(['x','y','r'])
Can be used as partial constructors.
var circle = new Circle().x(10).y(10).r(10);
var circle = new Circle().x(10).y(10);
var circle = new Circle().x(10);
Consider if Circle has a draw function.
var circle = new Circle();
circle.x(10).y(10).r(10).draw();
Define common parameters between objects.
var positioned = ['x','y'];
var dimensioned = ['width','height'];
function Circle(){}
Fluent.object(Circle)
.addProperties(positioned)
.addProperty('r');
function Rectangle(){}
Fluent.object(Rectangle)
.addProperties(positioned)
.addProperties(dimensioned);
var circle = new Circle().x(10).y(20).r(10);
var rectangle = new Rectangle().x(10).y(20).width(10).height(20);
Fluent attributes are available in the constructor of the object.
function A(){
this.test(42).test(); // === 42
}
Fluent.object(A).addProperty('test');
They are available in internal method definitions.
function A(){
this.doSomething = function(){
this.test(42).test(); // === 42
}
}
Fluent.object(A).addProperty('test');
They are available in prototype object extensions.
function A(){}
Fluent.object(A).addProperty('test');
A.prototype.doSomething = function(){
this.test(42).test(); // === 42
}
Jasmine tests available in the jasmine folder. Run SpecRunner.html.