Over time, I have become accustomed to utilizing the bind
method to store the previous result of a function and keep track of it for future use. This allows me to easily concatenate or join the previous string with a new string without needing external variables:
function remStr(outStr){
return function c(lastStr,newStr){
if(!newStr) return lastStr;
var all = lastStr+newStr;
return c.bind(null,all);
}.bind(null,outStr);
}
var str = remStr('stack');
str = str('over');
str = str('flow');
str(); // stackoverflow
The issue arises when I need to call remStr
multiple times, leading me to rely on bind
. Is there a more efficient or alternative way to achieve the same outcome as remStr
? Perhaps in certain scenarios, another approach may prove to be more effective than using remStr
?