Skip to content

Instantly share code, notes, and snippets.

@thunklife
Created April 15, 2013 17:26
Show Gist options
  • Select an option

  • Save thunklife/5389768 to your computer and use it in GitHub Desktop.

Select an option

Save thunklife/5389768 to your computer and use it in GitHub Desktop.
Something I stumbled upon for method chaining...along the lines of an Expression Builder, but without a named object.
//So, I came up with this when playing with an idea for a configuration DSL. It's like Expression Builders...I think.
//I think it's really like Partial Application with named functions.
//But is it rubbish?
//This is a contrived example for addition
var add = function(x){
//Right here is the basic pattern.
//Rather than return a function, return and object with a named function so your chaining becomes more grammatical
return{
to: function(y){
return x + y;
}
};
}
//which let's me write this
var result = add(10).to(20) //==> 30;
//or
var add10 = add(10);
var result = add10.to(20) //==> 30;
//Like I said, it's a stupid example, but the idea is ther.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment