Skip to content

Instantly share code, notes, and snippets.

@SergeyNarozhny
Last active September 30, 2016 07:37
Show Gist options
  • Save SergeyNarozhny/cbe55dc4f7ec2441d1be to your computer and use it in GitHub Desktop.
Save SergeyNarozhny/cbe55dc4f7ec2441d1be to your computer and use it in GitHub Desktop.
Native js function carrying realizations
//#1
var attitude = function(original, replacement, source)
{
return function (source)
{
return source.replace(original, replacement);
}
}
var slimify = attitude(/big test mark/ig, "smallone");
var happify = attitude(/sad/ig, "happy");
slimify("Big test mark is used"); // smallone is used
happify("Sadman is your Sandman"); //happyman is your Sandman
//#2
var param = "someVar";
var attitude = (function(i){
if (i == "someVar")
{
return function slimify(str)
{
return str.replace(/big test mark/ig, "smallone");
}
}
else
{
return function happify(str)
{
return str.replace(/sad/ig, "happy");
}
}
}(param));
attitude("Big test mark is used"); //smallone is used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment