Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

JavaScript doesn't have complete lexical scope. Consider:

    function foo() {
      console.log(whereDidThisComeFrom);
    }
    window.whereDidThisComeFrom = "the damn global object";
    foo();
And:

    function foo() {
      var a = {
        whereDidThisComeFrom: "a, of all places"
      };
      with (a) {
        console.log(whereDidThisComeFrom);
      }
    }
    foo();
In both cases, you can't lexically resolve the identifier whereDidThisComeFrom.


In the first case the lack of a whereDidThisComeFrom declaration in scope would imply that it meant `window.whereDidThisComeFrom` (in normal mode) or would imply a reference error (in strict mode) wouldn't it. Lua does the same thing and I never saw anyone complain about its lexical scoping.


Scheme does the same thing too:

scheme@(guile-user)> (define (foo) (write where-did-this-come-from)) ;;; <stdin>:1:14: warning: possibly unbound variable `where-did-this-come-from' scheme@(guile-user)> (define where-did-this-come-from "the damn global scope") scheme@(guile-user)> (foo) "the damn global scope"scheme@(guile-user)>

Munificent's assertion that this doesn't represent lexical scoping is wrong, but his assertion in another thread that with() is an exception to lexical scoping is correct, and therefore JS is not lexically scoped, just mostly lexically scoped.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: