Tuesday, September 21, 2010

Abusing? javascript functional powers

Javascript being a functional language makes it very easy to pass functions around as param to other function.

However when we do that we might lose readability of the code

Consider this:

mapcar1 is just another function, same as dojo.forEach in functionality.

The code in bold has lots of redirection and very hard to debug as it calls the callback if something is successful etc.

withTransforms : function (transforms, callback, errorCallback, config) {

if (reqNeeded) {

var trs = mapcar1 (
function (t) {

return {
"toSrs" : t.to
};
},
transforms);

var callbacks =
{
success : callback,
failure :this.cbWithTransformsFailed,
b4Success : this.cbB4SuccessWithTransforms,
scope : this
};

this.ajaxTool.post (
getWebappBaseUrl () + "/transformGeometry.do",
{
"transformRequests" : trs
},
callbacks,
config);

} else {

callback (
mapcar1 (
function (t) {

return t.geometry;
},
transforms));
}
},

No comments:

Post a Comment