6

I'm going down the es6 compatibility table trying to learn Here.

in the bindings section it says "block-level function declaration?". I'm unable to find any blogs or documentation besides the offical spec on that combinations of words.

Question: What is "block-level function declaration" referring to?

Armeen Moon
  • 18,061
  • 35
  • 120
  • 233

2 Answers2

5

the example kangax is testing for:

alert(function(){
    'use strict';
    function f() { return 1; }
    {
      function f() { return 2; }
    }
    return f() === 1;
}());

it means that function "hoisting" behaves the same way as let (vs var).

In ES5, braces were "decoration", unless they appeared after a few keywords like for, if, try, etc. so, the 2nd f() would "clobber" the 1st, but in ES6-compat runtimes, the 2nd f() is private to the block and thus doesn't replace the name f defined by the 1st function.

In ES6 braces ({ ... }) mean a block, even without a preceding keyword. That said, i don't see many arbitrary blocks in ES6 code, maybe just from lack of practice, ignorance, or perhaps just from lack of need; function scope works pretty well in JS.

dandavis
  • 16,370
  • 5
  • 40
  • 36
  • Re your last remark: I guess you're just not noticing the myriads of `for (let…) { … }` blocks any more, but they are definitely there :-) Admittedly function *declarations* inside them are sparse. – Bergi Mar 10 '16 at 08:08
  • @Bergi: my last remark continues my 2nd to last, so _arbitrary_ means non-ES5 blocks, for _scope_ only... – dandavis Mar 10 '16 at 09:17
1

Checkout SO What are the precise semantics of block-level functions in ES6? question.

You find a nice description of question and really verbose answer.

Community
  • 1
  • 1
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82