0

Given the following:

 function init() { return 1; }

how does parser parse

 new init();

?

I mean why init() function works together with new operator? Why function was not invoked independently?

  • you must do some basic research first start from here (http://stackoverflow.com/questions/4212149/init-function-in-javascript-and-how-it-works) – bananas Aug 17 '16 at 16:46
  • @AsteriskNinja: I don't see what that post has to do with the question. – Bergi Aug 18 '16 at 16:37

1 Answers1

0

Why function was not invoked independently?

Simply because that's the rule. new, init and the argument parentheses are four separate tokens, which together will form a constructor call. If new is not there, three tokens will form a normal function call.

To get it invoked independently, you could write new (init()) or new (init())() (not working of course as init() does not return a constructor).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375