Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ES6 what is new spec, "block-level function declaration" mean?

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?

like image 667
Armeen Harwood Avatar asked Mar 10 '16 06:03

Armeen Harwood


1 Answers

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.

like image 155
dandavis Avatar answered Oct 04 '22 04:10

dandavis