How can “new new Something” produce valid results in JavaScript?

前端 未结 3 2075
无人共我
无人共我 2021-01-12 21:30

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

3条回答
  •  萌比男神i
    2021-01-12 21:56

    Take a look at section 13.2.2 [[Construct]] in the specification. More precisely, step 9 of the algorithm.

    1. If Type(result) is Object then return result.

    Functions are objects, so if you return a function from a function and try to instantiate the latter function using new, you'll get back the former function, not a new object. But functions are also constructors, so you can new the returned function.

    function bar() {}
    function foo() { return bar; }
    new foo === bar; // true
    

提交回复
热议问题