1

I've seen this a few times - in a top level function an old programmer at my job used to do the following:

function x() {
  return new function() {
   //some code
  }
}

What is the difference between that and:

 function x() {
  return function() {
   //some code
  }
}

Wouldn't returning an anonymous function without new instantiate a new function every time? It's not currying, so I'm not sure I see a difference.

Thanks

  • 4
    `return new function() { ... }` is NOT idiomatic and is, frankly, pretty weird – apsillers Feb 11 '20 at 20:54
  • 3
    The difference is that the [`new`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new) operator returns an object, not a function. – Tyler Roper Feb 11 '20 at 20:57
  • @WiktorZychla I saw that, but it doesn't answer the latter part of the question –  Feb 11 '20 at 21:00
  • 1
    It's not weird in any way, it's just `new` on anonymous function. And yes, returning anonymous function returns a new function every time. – Wiktor Zychla Feb 11 '20 at 21:00
  • @TylerRoper That makes perfect sense. Thank you. Is it bad practice to do that? –  Feb 11 '20 at 21:01
  • The two examples you've shown serve two vastly different purposes, but I'm not sure if I'd call either "bad practice" without knowing the context. `new function()` is an alternative way to create [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). The second is just returning another function. – Tyler Roper Feb 11 '20 at 21:03
  • can you please confirm it is `return new function()` and NOT `return new Function()`, if it is `Function` then see this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function > Difference between Function constructor and function declaration – Mahabubul Hasan Feb 11 '20 at 21:06
  • Nope, I know about the Function constructor which allows you to create functions on the fly. @TylerRoper answered it. It was basically an unintuitive way to make a class –  Feb 11 '20 at 21:10
  • It's an especially weird way to make a class, since the main benefit to a class is that you can do `SomeFunc.prototype.foo = ...` which will make `foo` accessible on every `new SomeFunc` instance. You can't do that with an anonymous function never stored to a variable, though. I see no reason why you'd do `new function() { this.bar = ... }` versus the more idomatic `var myObj = {}; myObj.bar = ...` – apsillers Feb 11 '20 at 21:19
  • @apsillers this could be used as a sort of "private constructor" where you can't (or it's harder to) instantiate other instances of this. It's honestly not too helpful unless there is something missing here. The only way to make new instances is either `a = x()` or `b = new a.constructor`. However, it's starting to become bizarre, since `a` and `b` share a constructor now but they don't share a constructor with `c = x()`. Overall, this *might* serve a purpose but it's more likely to be a mistake or misunderstanding. – VLAZ Feb 11 '20 at 21:24
  • It's a singleton. If we can reopen this I will put this as the answer. Using "new function(){}" is almost the EXACT same thing as creating an object with function properties "let apple = {xyz: function() {}}". The benefit of "new function" is you have slightly more freedom in declaring variables and such (you can't even use the prototype). But you could simply do "return { xyz: function() {} }" and get the same anonymous singleton. https://www.phpied.com/3-ways-to-define-a-javascript-class/ –  Feb 12 '20 at 02:52

1 Answers1

0

The first function return a value of type "object" and the second return a value of type "function".

I think this piece of code can help you

console.log(typeof(function(){})) // "function"
console.log(typeof(new function(){})) // "object"

Two type of differents value so the behavior is totally different.

tony95
  • 161
  • 1
  • 10