Passing variables into a function before and after it is called in JavaScript (Partial Application)
Lately I’ve been working on a client/server side validation library. I had a need to chain a bunch of methods together, and what that meant was a need to modify how the original functions were called without changing them. This lead to a need to pass some variables into a function and still wait to accept more parameters later. I already knew about currying, which I will summarize as being able to pass as many variables as you feel into a function. A couple other techniques were needed to solve this.
First I learned about the apply and call functions (I know I am late to the party…). In this case apply was extremely helpful, it let me set a context (change the value of this) and collect some parameters together and pass them into a function.
Next I discovered partial applications. This is more of a technique than anything else. In iz we have the following source code:
function validator_partial(fn) { //get all arguments except the first, which is the function name var args = Array.prototype.slice.call(arguments, 1); //pass the "value" in as the first parameter so that the user of this library doesn't need to args.unshift(value); //return a new function return function() { //combine all arguments made to this function with the ones above var allArguments = args.concat(Array.prototype.slice.call(arguments)), //get the result result = validators[fn].apply(null, allArguments); //update this object if (!result) { if (typeof this.error_messages[fn] !== "undefined") { this.errors.push(this.error_messages[fn]); } else { this.errors.push(fn); } this.errors.push(fn); this.valid = false; } //return "this" to allow for chaining of methods return this; }; } for (var fn in validators) { //for each function, call the partial and pass in the function if (validators.hasOwnProperty(fn)) { Iz.prototype[fn] = validator_partial(fn); } }
At the bottom I am grabbing the validators, and assigning them to the prototype of the Iz object. Before the validators get called though they go through a partial. This partial is a function that returns a function. The closure allows you to house some variables within it. When the returned function gets called it has access to the outer one’s scope, which is how you are able to pass in parameters before and after the function is called. On top of that this system lets you pass in as many params as you need and it simply forwards everything on.
With this method I was able to replace all of the first parameters (the ‘values’ with the value from the Iz object. This means less typing, which is always nice!
Leave a Reply
You must be logged in to post a comment.
Recent Posts
- ReactJS workflow with Mocha, JSDom, Gulp, Browserify, CoffeeScript and SublimeText
- Analysis of Spotify’s Discover Page
- Pro tip: DONT use mso conditionals for emails
- Passing variables into a function before and after it is called in JavaScript (Partial Application)
- Javascript Namespacing/Classes/Debugging etccc….
- A fresh start
Archives
- August 2014
- September 2013
- October 2012
- August 2012
- July 2012
- January 2011
- September 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- June 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
WordPress, Entries (RSS) and
Comments (RSS)