chaining   101

« earlier    

repeatingbeats/invoke - GitHub
RT @ded: Neat control flow library for Node - long live chaining flows
chaining  flow-control  javascript  nodejs 
february 2012 by syntaxterror
Sugarless - A Functional & Context Oriented way to write JavaScript | LakTEK
Sugarless is a more expressive way to write functional and context-oriented programs in JavaScript.

Imagine a case where you have an input which has to undergo certain operations to produce a meaningful output. We define each operation as a function. The common way to do this in JavaScript would be:

var output = truncate(trim(sanitize(input)), 200)
At a glance, it's not very easy to comprehend. It gets further complicated if we try to add more functions or remove a certain function.

If we think in terms of Object Oriented concepts we can try to make this more readable with a fluent interface (ie. method chaining).

var output = input.sanitize()
.trim()
.truncate(200);

In order to do this in JavaScript, we must make sure the functions are available in the input object's prototype chain, object itself is mutable and all functions, apart from the last function returns the input object. Though this can be done, it's doesn't feel very natural or flexible.

Using Sugarless, this is how we can write it in a more readable and flexible manner:

var output = Sugarless(input)(
sanitize
, trim
, truncate, "", 200
);
javascript  programming  library  sugarless  npm  node.js  chaining 
january 2012 by jasonw22

« earlier    

Copy this bookmark:



description:


tags: