0

Let's say there are two ways to define a function:

First way:

Bar.prototype.foo = function ()
{
  // do something
}

Second way:

Bar.prototype.foo = function ()
{
    return function () {
        // do something
    }
}

For sure the second way is useful in case of closure.

There are other case where the second way is preferable to the fist one?

antonjs
  • 14,060
  • 14
  • 65
  • 91

2 Answers2

1

I have had a project in where I dynamically built validator functions for certain input types in a way similar to the closure case.

So returning different function based on the input parameters might be a valid usecase for the later way to define a function.

Sirko
  • 72,589
  • 19
  • 149
  • 183
1

You can use it do y combinator style work, see this answer: https://stackoverflow.com/a/94056/426894

Community
  • 1
  • 1
asawyer
  • 17,642
  • 8
  • 59
  • 87