I'd argue that the things that are wrong with "dynamic scope" in the Lisp sense (usage #1) are just as wrong with "non-static scope" (usage #2). Dynamic scope in Lisp was anti-modular because the meaning of a variable could be affected by any code throughout the arbitrary control flow of the program. The same is true whenever you have variables whose bindings are decided by dynamic control flow. Lisp's dynamic binding was just a special case of the general phenomenon of variables whose bindings are determined dynamically. This is why, in my mind, the distinction isn't actually all that important.

Perhaps the example below helps: it contrives to emulate dynamic scoping by dynamic (global) binding, complete with emulating local, stack-backed bindings, using temporary
(global) bindings plus static stack.

Claus


function log(msg) {
 if (typeof console!=='undefined')
   console.log(msg);
 else if (typeof WScript!=='undefined')
   WScript.Stdout.WriteLine(msg);
}

function B(l) {
log(l+': x is '+x); // x is always global, but may not exist there;
 //
 // that still isn't static scoping; it is "just"
// a question of whether x is bound, not where // it is bound, but the answer is not static.
 //
 // (if x isn't there, that is a reference error,
 // not an undefined)
}

if (Math.random()>0.5)
 this.x = "hi";  // now you see it, now you don't
                 // dynamic binding with global scope

if (typeof x=="undefined") {

 this.x = "ho";
 B("local"); // emulate dynamic scoping by dynamic binding
 delete this.x;

} else { // x is defined

 B("global");

 (function(){
   var old_x = x; // use lexical stack to backup x
   delete x;      // shadow x

   try { B("gone 1"); } catch(e) { log('x no longer defined'); }
x = "temporary"; // emulate a local binding by // a temporary global binding

   B("dynamic"); // dynamic scoping, in all but name

   delete x;         // local/temporary binding ends
   try { B("gone 2"); } catch(e) { log('x still not defined'); }

   x = old_x;    // restore x
 }());

}

B("main");


_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to