Are all function declarations & expressions created by called new Function() behind the scenes?

88 Views Asked by At

I'm reading the portion of ECMA 262 v5 script that speaks of Function definitions. For both function declarations and function expressions, the following is mentioned:

Return the result of creating a new Function object as specified in 13.2

The first step of what is mentioned in section 13.2 is to "create a new native ECMAScript object..." and then "Set the [[Class]] internal property of F to "Function".

Due to the wording of "creating a new Funciton object", I was wondering if behind the scenes, function declarations/expressions were created by calling new Function(....)?

2

There are 2 best solutions below

0
On

section 15.3.2.1 of the ECMA Script specification speaks about the new Function(...) expression, and the new Function(...) expression itself refers to the same logic as specified in section 13.2.

Bullet number 11 about the new Function(...) expression specifies...

Return a new Function object created as specified in 13.2 passing P as the FormalParameterListopt and body as the FunctionBody. Pass in the Global Environment as the Scope parameter and strict as the Strict flag.

Section 13.2 is the single source of function creation logic, whether creating a function expression, function declaration, or invoking the Function constructor.

So to answer the question, no. Creating a function declaration/expression doesn't call new Function(...).

0
On

Due to the wording of "creating a new Funciton object", I was wondering if behind the scenes, function declarations/expressions were created by calling new Function(....)?

No. It does exactly what is says, creating a new object and making it a function. What would you expect to happen behind the scenes of new Function else? Instead, new Function does call that function instantiation in 13.2 as well.