Search code examples
javascriptecmascript-5

How can "new new Something" produce valid results in JavaScript?


I'm currently developing a JavaScript parser and study the ECMAScript 5.1 specification. Here's a question which puzzles me at the moment.

§ 11.2 Left-Hand-Side Expressions defines the following NewExpression production:

NewExpression :
    MemberExpression
    new NewExpression

If I read it correctly, then the NewExpression may be something like

new new Something

(Actually, any amount of news.)

This puzzles me completely. How could new Something potentialy return anything you could once again new? Is it possible at all?


Solution

  • It is not common at all, but it is possible; a function that returns a function:

    function baz(){}
    function foo(){return baz}
    
    new new foo() instanceof baz // true