Created
September 9, 2011 15:56
-
-
Save kapusta/1206584 to your computer and use it in GitHub Desktop.
A non-polluting-recursive-function inside a Javascript module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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