Details about what happens when entering a function declared in the global scope is missing from ECMAScript Specification v5?

StackOverflow https://stackoverflow.com/questions/15123595

Question

The ECMAScript specification goes into detail about what happens when control enters the execution context of a function within a function.

function foo() {
  function bar() {

  }
  bar(); // Control will be given to the bar function.  Details specified by spec
}

There also is an explanation of what happens when control enters global code.

<script>
  // Entering global code!  Details specified by spec
</script>

However, there is nothing specifying what happens when entering control for a function defined in the global code.

<script>
  function foo() {
  }
  foo(); // Calling a function defined in the global environment...not specified by spec
</script>

Edit: The reason this is important to me is because I'm curious what the internal [[Scope]] property of the function called by the global code will be. I assume it will be the lexical environment of the global execution context, but there's nothing that specifies this in the specification.

Was it helpful?

Solution

I think you misinterpreted that sentence (from §10.4.3, Entering Function Code):

The following steps are performed when control enters the execution context for function code contained in function object F, a caller provided thisArg, and a caller provided argumentsList […]

It does not mean that the function which is entered must be contained in F, but that the code that is entered is contained in the function F (which you are invoking).

The [[Call]] method which is used when calling a function does not distinguish between global/local declared or invoked functions.

OTHER TIPS

Functions declaraed in global code are instantiated during Declaration Binding Instantiation of the global code in step 2 of 10.4.1.

This is done via step 5.c of 10.5 which actually creates each such function object via the first algorithm in section 13. Note that this sets the [[Scope]] of the function to the VariableEnvironment of the current execution context. The current execution context was set by step 1 of 10.4.1 (via 10.4.1.1) to the Global Environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top