- Implicitly returns itself from all decendant methods
var chain = chainlang.create({
returnsNothing: function(){
return;
},
levelTwo: {
returnsNothing: function(){
return;
}
}
});
expect(
chain().returnsNothing()
).to.eql(
chain()
);
expect(
chain().levelTwo.returnsNothing()
).to.eql(
chain()
);
- Will return any explicit return value instead of returning itself implicitly
var chain = chainlang.create({
returnsSomething: function(){
return 'something';
}
});
expect(
chain().returnsSomething()
).to.eql("something")
- contains a "_data" property which can be used to pass data through the chain and is initially empty
var link = chainlang.create({})();
expect(link).to.have.key('_data');
var hasOwnKey = false;
for(var key in link._data){
if(link._data.hasOwnProperty(key)){
hasOwnKey = true;
}
}
expect(hasOwnKey).to.be(false);
- contains a "_subject" property, which references the optional parameter to the chain function
var chain = chainlang.create({
getSubject: function(){
return this._subject;
}
});
var actualSubject = {iAm: "theSubject"};
expect(
chain(actualSubject).getSubject()
).to.be.eql(actualSubject);
- allows chaining of methods arbitrarily nested within properties of the language
var chain = chainlang.create(
{
addOne : function(){
this._subject = this._subject + 1;
},
secondLevel : {
addOne : function (){
this._subject = this._subject + 1;
},
thirdLevel : {
addOne : function(){
this._subject = this._subject + 1;
}
}
}
});
expect(
chain(1).addOne()._subject
).to.be(2);
expect(
chain(1).addOne().secondLevel.addOne()._subject
).to.be(3);
expect(
chain(1).addOne().secondLevel.thirdLevel.addOne()._subject
).to.be(3);
expect(
chain(1).addOne().secondLevel.addOne().secondLevel.thirdLevel.addOne()._subject
).to.be(4);
- is bound to "this" for every method call in the chain, even for methods of child objects
var spy = sinon.spy();
var chain = chainlang.create({
topLevelFn: spy,
secondLevel: {
secondLevelFn: spy
}
});
var theChain = chain();
// Running some methods to spy on their `this` value
chain().topLevelFn().secondLevel.secondLevelFn();
expect(spy.thisValues[0]).to.be.eql(theChain);
expect(spy.thisValues[1]).to.be.eql(theChain);