4

function(){}.__proto__ === Function.prototype and Function.prototype === function(){}.__proto__ get different result

Function.prototype === function(){}.__proto__ return true.

function(){}.__proto__ === Function.prototype return an error:

VM2053:1 Uncaught SyntaxError: Function statements require a function name

m0tive
  • 2,796
  • 1
  • 22
  • 36
  • 2
    Wrap it with parentesis to make it an expression `(function(){}).__proto__ === Function.prototype` – Yury Tarabanko Aug 30 '19 at 09:08
  • yes,when wrap it with parentesis ,it`s ok. i don`t understand is there any different put `function(){}` in the `===` left or right? – microbingbing Aug 30 '19 at 09:14
  • 2
    `function` in the beginning is function declaration statement. Try `function a(){}.__proto__` Almost the same as `[] === {}` vs `{} === []` where `{}` is not an object literal but a block statement when appears in the beginning. – Yury Tarabanko Aug 30 '19 at 09:17
  • 1
    So `function(){}.__proto__ === Function.prototype` effetively means `function(){}; .__proto__ === Function.prototype;` notice `;` – Yury Tarabanko Aug 30 '19 at 09:22
  • o, i see, thank you very much! – microbingbing Aug 30 '19 at 09:31
  • No problem. You could also use [ast explorer](https://astexplorer.net/#/gist/51fab4c0ed6fe2484b967402aaecd5b5/05534541037c8c3fef53d674f78a32a46a2f1bbf) with say tsc parser (that wont bail on the first error) to check how the code is parsed. – Yury Tarabanko Aug 30 '19 at 09:35
  • @yury would make sense to add that as an answer, wouldn't it ...? – Jonas Wilms Aug 30 '19 at 09:51

2 Answers2

3

Because function declarations must have a name, where as function expressions do not. From the wiki page on Immediately invoked function expression, in the case where the line starts with function:

when the parser encounters the function keyword, it treats it as a function declaration (statement), and not as a function expression.

m0tive
  • 2,796
  • 1
  • 22
  • 36
  • This is definitely the case. The first situation is interpreted as an expression, while the second is interpreted as a declaration. – Travis J Aug 30 '19 at 19:20
0

(function () {}).__proto__ === Function.prototype return true.

Function.prototype === (function () {}).__proto__ also return true;

xu X
  • 11
  • 1