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