What are the benefits of using closures in JavaScript and why should I consider them in my code if I can usually write a more simple and straightforward solution, at least in my opinion.
For example, consider this:
function multiplier(factor) {
return function(number) {
return number * factor;
};
}
var twice = multiplier(2);
console.log(twice(5));
//////////////////////////////////////////////////////////////////
function myMultiplier(factor, number) {
return number * factor;
}
console.log(myMultiplier(2, 5));
They both output 10, but I find myMultipler easier to understand, quicker to write, and I only needed one function to do it. Why should I consider the closure version over my-version?
Thanks in advance.