First, in order to make this clear, notice that Promse.constructor
is just a reference to the Function
constructor.
So basically you are just calling:
Function.call(this, function () {})
and:
Function.call(this, () => {})
The first expression throws because function () {}
is not a valid function statement (but it's a valid function expression).
The problem is that being at the top block of the scope, the function
identifier is matching the grammar of the statement form of the function declaration, and function statements should be named (that's why the error says (
is an "unexpected token", the parser it's expecting a name between the function
keyword and the (
character).
If you do use a name, or use the comma operator, it will treat the function as an expression:
console.log(Function('0,function () {}'))
console.log(Function('function test() {}'))
With arrow functions there is no problem since they can only be expressions.