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!).