1

In static scoping, the identifiers can be determined by analyzing/parsing the source code (versus dynamic scoping which needs to, more or less, know the callers environment).

My question is this, since static scoping simply needs to parse the source code to know the scoping and identifier bindings, is the scope environment/identifier bindings made at compilation time, or are these bindings determined when each function (or new scope) is accessed.

JavaScript example:

var globalVar;
function fooFunc() {
  var fooVar;
  function barFunc() {
    var barVar;
  }
}

In this simple JavaScript example, I can visually determine by examining the program that the barFunc environment will have a variable of barVar, and that has a parent environment that has fooVar as well as the barFunc function, and that has a parent environment of the global scope which has globalVar. I'm curious if the compiler/interpreter reads the source code as a whole and makes the bindings, or if these determinations are made at a function by function (or scope by scope) basis.

If I am unclear, please let me know.

Thank You,

Edit: Although my example is in JavaScript (which I'm particularlly curious about), I also am curious if this differs between languages (i.e. JavaScript, C/C++, C#/VB, etc.)

contactmatt
  • 18,116
  • 40
  • 128
  • 186

2 Answers2

0

In JavaScript a scope is only created when a function is called. Hence although your example has a function inside a function declared in the global scope only the global scope exists.

The fooFunc scope and the barFunc scope do not exist until fooFunc and barFunc are called. Since they are never called the global scope is the only scope in the program. Oh, and barFunc doesn't even exist since fooFunc was never called.

When you call a function in JavaScript a new execution context is created for it. An execution context can be thought of as the scope of the function. See this answer for more details: https://stackoverflow.com/a/9384894/783743

BTW you can simulate dynamic scoping in JavaScript too: Is it possible to achieve dynamic scoping in JavaScript without resorting to eval?

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • Do you know if a complied language, (i.e. C/C++) makes these bindings at runtime or compilation time? – contactmatt Feb 22 '13 at 16:17
  • Space for local variables in C/C++ is usually reserved on the call stack. I don't know the internals but my guess would be that the offsets to parent scopes are determined at compile time, however the actual addresses can only be determined at run time when the function is actually called. – Aadit M Shah Feb 22 '13 at 16:22
0

I believe there is no compiler for Javascript and I don't want to trigger a discussion.

Coming to your question, the body of the function is never executed when the Javascript is downloaded to the browser. It needs to be explicitly invoked. So there is no way the variable binding can be made unless the function is invoked.

hop
  • 2,518
  • 11
  • 40
  • 56