Skip to content

Instantly share code, notes, and snippets.

@kapusta
Created September 9, 2011 15:56
Show Gist options
  • Save kapusta/1206584 to your computer and use it in GitHub Desktop.
Save kapusta/1206584 to your computer and use it in GitHub Desktop.
A non-polluting-recursive-function inside a Javascript module
var o = {}; /* namespace */
(function(undefined) {
this.trimPeriods = function(s) {
var return_string; /* private to trimPeriods() */
(function killCrushDestroy(s) { /* a non-polluting-recursive-function (that trims periods from the end of a string) */
if (s.substr(s.length - 1, 1) == ".") {
killCrushDestroy(s.substring(0, s.length - 1));
} else {
return_string = s; /* if we did a return here, we wouldn't be returning to the original caller */
}
})(s); /* pass the arg sent to trimPeriods() into killCrushDestroy() */
return return_string; /* send return string back to whoever called us */
};
}).apply(o); /* apply everything in here to the "o" object */
alert(o.trimPeriods("blah...")); /* alerts "blah" */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment