block scope and function declarations

Andy Wingo wingo at igalia.com
Wed Feb 15 03:55:01 PST 2012


Hello again ecmascriptians,

There has been some discussion about treating function declarations as
"var" declarations.  While I understand the concerns about
compatibility, I think it will be surprising to programmers.

For example:

  function f(x) {
    bar(); // ???
    for (let y of x) {
      function bar() { ... y ... }
      bar(); // OK
    }
    bar(); // ??? 
  }

Here I think it's pretty natural to expect that the FunctionBody of
`bar' has access to lexically scoped binding for `y'.  But outside the
block, the function bound to `bar' doesn't have any meaning.

I only see two consistent answers here:

  function foo(){}  ==  let foo = function(){}

or

  funciton foo(){}  ==  var foo = function(){}

Hoisting the function definition doesn't make sense with block scope.

Andy


More information about the es-discuss mailing list