Why can I use a function before it's defined in JavaScript?

前端 未结 7 1812
日久生厌
日久生厌 2020-11-22 16:07

This code always works, even in different browsers:

function fooCheck() {
  alert(internalFoo()); // We are using internalFoo() here...

  return internalFoo         


        
7条回答
  •  春和景丽
    2020-11-22 16:45

    Some languages have the requirement that identifiers have to be defined before use. A reason for this is that the compiler uses a single pass on the sourcecode.

    But if there are multiple passes (or some checks are postponed) you can perfectly live without that requirement. In this case, the code is probably first read (and interpreted) and then the links are set.

提交回复
热议问题