3

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.

Ramson
  • 229
  • 4
  • 12
  • In this particular case there is no much reason to do that unless some contract requires a `smokerDisabled` to return a function. – zerkms May 27 '15 at 10:03
  • I don't think this example is good, but the question is interesting. – kemicofa ghost May 27 '15 at 10:08
  • possible duplicate of [How do JavaScript closures work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Hacketo May 27 '15 at 11:03

7 Answers7

5

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.

jered
  • 11,220
  • 2
  • 23
  • 34
4

In this particular case you do not have any advantage in the first version w.r.t. the second one.

But there are many situations in which it can be useful. What you can do is to use closures to store data.

Let's modify your example a little bit. Let's create a customizable threshold

function createAgeFilter(minAge) {
    return function (age) {
        return age > minAge;
    }
}

So you can do

var filter = createAgeFilter(15);
...
if (filter(user.age)) { ... }

Instead you should do

function filter(age, minAge) {
    return age > minAge;
}

and so

if (filter(user.age,15)) { ... }

This helps if you want to avoid to repeat 15 every time you have to filter or you want to be able to change the filter implementation without the need to update every call to the filter function.

You can find other examples here.

http://www.bernaschina.com/en/blog/software/nodejs-code-reuse-right-way/

B3rn475
  • 1,057
  • 5
  • 14
2

All depends on your needs, the first is used by calling f.smokerDisabled()(15), and the second is called using f.smokerDisabled(15). It might be used in closure.

The first one :

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

var f = new form();
f.smokerDisabled()(15); // returns : disabled="disabled"

The second one :

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

var f = new form();
f.smokerDisabled(15); // returns : disabled="disabled"
Xakiru
  • 2,556
  • 1
  • 15
  • 11
1

When a function is returned, it's called a closure. This would be useful for when you have use private variables. So, in your example, say your code was like this:

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

This means, smoker variable will be unavailable to other variables. In this instance, it might be hard to see the logic behind it but imagine you're having a guess-the-number game.

function guesser(){
  var number = Math.floor(Math.random()*10) + 1;
  return function(guess){
      return guess === number ? "You got it!, it was " + number : "Failed - guess again";
  }
}

So, now you can do:

var round1 = guesser();
round1(3); // "Failed - guess again"
round1(5); // "You got it!, it was 5"

var round2 = guesser();
round2(5); // "Failed - guess again"

Each new call of guesser(), creates a new random number between 1-10 where you can't access from outside (i.e: you can cheat!).

Bwaxxlo
  • 1,860
  • 13
  • 18
1

In your code, there is not really anything it might achieve, but with this:

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

You can use the parameter of the first function to produce another function.

Producing a function in a function is at the base of functionnal programming.

And using the parameter(s) of a function to produce another function is calling currying.

laruiss
  • 3,780
  • 1
  • 18
  • 29
1

Your question:

what is the use of keeping the actual code inside another function and returing it?

As in the comments and in answers you've got suggested that in your specific example case this isn't needed.

But if you have a inner function in a outer function's scope then you have to return it to get the result of it.

What i meant is, let say this function which would not work if you remove the return of the inner function:

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

so if you call smokerDisabled method it would not return anything because it has a inner function which returns the result not the outer function.

So if you have decalared an inner function in outer function then you have to return the inner function which returns the result of some operation.

Jai
  • 74,255
  • 12
  • 74
  • 103
-2

After googling:

It is called closure.

Basically, the function defined within other function is accessible only within this function. But may be passed as a result and then this result may be called.

It is very powerful feature. You can see more explanation here:

http://web.archive.org/web/20120101221030/http://blog.morrisjohns.com/javascript_closures_for_dummies.html

Hope this helps to explain it

Razieltje
  • 357
  • 1
  • 7
  • Closure is a different approach to make any parent function's variable or object private to the returning function. But in this case, I dont see that kind of requirement. – Ramson May 27 '15 at 10:08