0

the following snippet of code throws a ReferenceError: selectTen is not defined

    var select = _.pipe(
      selectTen,
      fetchUrls,
      awaitUrlData,
      connectToLoad
    )

    var selectTen = _.partialRight(selectItems, [4]) 
    var connectToLoad = _.partialRight(connect, [load]);  

While the following snippet runs perfectly fine:

   var selectTen = _.partialRight(selectItems, [4]) 
   var connectToLoad = _.partialRight(connect, [load]);  

    var select = _.pipe(
      selectTen,
      fetchUrls,
      awaitUrlData,
      connectToLoad
    )

I am running this code as part of an angularjs project using vanilla (ES5) JavaScript where .js files are being included in tags in index.html. No module loader is being used

the _. notation is part of lodash' functional programming library. Suffice to say that they return functions and could be replaced with function(param){...}.

Why are the selectTen and connectToLoad functions not being hosted in the first snippet?

matthiasdv
  • 1,136
  • 2
  • 15
  • 26
  • @SurenSrapyan i don't see the correlation between this question and the one you have marked. – Scott Marcus Jan 10 '17 at 15:01
  • 2
    Why should assignments be hoisted? `const` is not part of ES5 btw. – a better oliver Jan 10 '17 at 15:03
  • 1
    *... vanilla (ES5) JavaScript* — but ES5 didn't have `const` declarations? – Pointy Jan 10 '17 at 15:03
  • @zeroflagL whoa, you're actually right about const not being part of ES5 – matthiasdv Jan 10 '17 at 15:05
  • @Pointy I edited my question accordingly. Still don't see why I see people using the form const myFunction = (x) => x+x in ES6? This isn't hoisted? – matthiasdv Jan 10 '17 at 15:09
  • @matthiasdv hoisting with `const` and `let` is kind-of weird. With `var` hoisting, the symbol was defined in its scope as if declared at the beginning, but only initialized at the point of *actual* declaration. The rules for `const` and `let` make that an explicit error, because it tended to cause lots of bugs. – Pointy Jan 10 '17 at 15:11
  • 1
    Possible duplicate of [Are variables declared with let or const not hoisted in ES6?](http://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-not-hoisted-in-es6) – Rudi Jan 10 '17 at 15:11

2 Answers2

2

Like let declarations, const declarations are scoped to their enclosing block as if the variable (well, the constant) were declared at the top of the block. However, the value can't be used until the point in the code where the declaration actually appears.

The gap between the opening of the block and the point at which the declaration appears is called (rather dramatically) the "temporal dead zone".

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

Because they are not defined functions, they are constants that have a function literal as their value.

If you'd do e.g.:

function selectTen(selectedItems) { return _.partialRight(selectedItems, [4]); }

then they would be hoisted

Ovidiu Dolha
  • 5,335
  • 1
  • 21
  • 30