1

I had a javaScript function with many nested functions. First I declared all my functions like function funcName() {...js code} and everything was fine. Then I've created a React app that uses ES6 syntax, such as arrow functions. Now I'm trying to import my well tested javaScript function to the React app. I changed all that nested functions to arrow functions with the same content to keep using ES6 syntax. Now when any of that functions is called, I get an error: 'Cannot access 'circlesByPath' before initialization'. This one is for circlesByPath func, when I change it back to normal function syntax, I don't get this error. Can anyone answer why is this happening? I previously considered arrow functions to be only a modern way to declare function, I didn't use arrow functions in vanilla javascript, only in my React app. Now I see something is wrong with them...


I returned function keyword to every function. Now The problem is "ReferenceError: holesNum is not defined" It happens every time when script uses variable that was meant to be global (declared with no 'var', 'let' or 'const' keywords), it worked before (without React), now all variables are undefined...

Mikhail
  • 57
  • 1
  • 7
  • Please add some code for illustrate your problem – dolor3sh4ze Dec 18 '20 at 13:43
  • 2
    "*I previously considered arrow functions to be only a modern way to declare function*" 1. [Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?](https://stackoverflow.com/q/34361379) 2. [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/q/336859) – VLAZ Dec 18 '20 at 14:01
  • Thank you, VLAZ! This answers my question! The only issue now is that I have to declare all variables used in nested functions to make them global, before I declared them without "var" and it was OK, now this gives an error ReferenceError: variableName is not defined – Mikhail Dec 18 '20 at 14:02
  • `function` declarations are totally valid ES6 and, apart from the different behavior, also even shorter than `const … = () => {` – Bergi Dec 18 '20 at 14:49

0 Answers0