Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of javascript function returing another function

I see so many functions like this:

form.prototype.smokerDisabled = function(){
  return function(age) {
    return age>=15 ? 'disabled="disabled"' : '';
  };
};

what is the use of keeping the actual code inside another function and returing it. Instead it would have been written:

form.prototype.smokerDisabled = function(age){
  return age>=15 ? 'disabled="disabled"' : '';
};

Please tell me the difference and use of the first approach. Thanks.

like image 357
Ramson Avatar asked Jun 10 '26 15:06

Ramson


1 Answers

In this case I don't think there's any reason you would do that. The reason you would normally see a return function like that is to cause a closure that captures the state of an outside variable.

like image 68
jered Avatar answered Jun 12 '26 04:06

jered



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!