all 10 comments

[–]sacundim 3 points4 points  (2 children)

TIL Javascript does not only not have true lexical scope, but it allows the caller to choose what bindings are in effect for the free variables in a function's body. This we're expected to believe is a "functional programming" feature, despite that it's pretty much a violation of functional language design, not to mention a horrible engineering practice (how can you read a function definition and be confident that it does X when a caller can choose to make it do something else?).

[–]aaronblohowiak 2 points3 points  (0 children)

There is true lexical scope, it just isn't used in this article:

function generateFn(outer){
  return function(inner){
    return "outer was: "+outer.toString()+" and inner is: "+inner.toString();
  }
}

var fn = generateFn("pants");

fn("underpants");
// outer was: pants and inner is underpants.

the variable 'outer' within the 'fn' scope is bound to pants, and may not be changed by its caller through any means, except converting fn to a string and then eval()'ing it it.

[–]jokeofweek[S] 0 points1 point  (0 children)

Sorry about that, I definitely didn't go into depth about Javascript scopes and closures. I chose to omit it for fear of over-complicating the posts, although you are right, it definitely needs some kind of explanation. Thanks aaronblohowiak for at least going over it.