question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function defined inside conditionals can't be accessed outside

See original GitHub issue

What ?

Sample code:

 const someCondition = true;
 if (someCondition) {
   function insideFunc() {
     console.log('inside func');
   }
 }

 function someOtherFunc() {
   if (someCondition) {
     insideFunc();
   }
 }

After babelifying gives output like this:

var someCondition = true;
if (someCondition) {
  var _insideFunc = function _insideFunc() {
    console.log('inside func');
  };
}

function someOtherFunc() {
  if (someCondition) {
    insideFunc();
  }
}

Notice the variable insideFunc this has been renamed inside the conditional with a suffix _ but the reference isn’t updated outside that conditional i.e inside someOtherFunc. This code throws an error when executing someOtherFunc, saying insideFunc is not defined.

My configurations

Package versions: screen shot 2018-03-08 at 8 11 56 pm

Gulp task screen shot 2018-03-08 at 8 12 36 pm

My attempt

  1. the above is fixed if I define the insideFunc like this
 var insideFunc = function() {
};
  1. If i exclude es2015 from presets in gulpfile, then the function names comes correctly

What could be permanent fix for this ?

Really appreciate any help 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
loganfsmythcommented, Mar 9, 2018

The issue is that in JS versions before ES6, function declarations inside of if blocks (and any non-function-body) actually weren’t allowed at all and were not part of the specification.

Unfortunately, most of the engines had them anyway, but every engine treated them slightly differently. The “optional” feature I mentioned is the subset of behavior of all of the core engines that all behaved the same. It was specified as optional because it exists to define the behavior of existing engines in a standard way, not to be a requirement for future engines. That is also why the optional behavior is only for non-strict code.

1reaction
loganfsmythcommented, Mar 8, 2018

This issue is tracked in https://github.com/babel/babel/issues/5266 but it’s not so much a bug as it is just currently unsupported and potentially something we won’t support.

The issue is that

const someCondition = true;
if (someCondition) {
  function insideFunc() {
    console.log('inside func');
  }
}

is like you had written

const someCondition = true;
if (someCondition) {
  let insideFunc = function() {
    console.log('inside func');
  };
}

so the insideFunc variable only exists within the scope of the if block.

So why does your code work without the es2015 transform? Because the specification allows this to optionally work, when code is not running in strict mode. Since it is optional, and quite complex, Babel doesn’t currently support it, and I honestly can’t say if we will or not.

The best thing to do would be to rewrite your code as

const someCondition = true;
let insideFunc;
if (someCondition) {
  insideFunc = function() {
    console.log('inside func');
  }
}

function someOtherFunc() {
  if (someCondition) {
    insideFunc();
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

[solved]Why I cant use a variable outside the function defined ...
you have to declare the variable outside the function to use it. variables declared inside a function can only be accessed inside that...
Read more >
c++ stucts cant be accessed outside the if statement
The goal is to have the user input some movie information for two movies. The problem is when I try to use the...
Read more >
Can not use variable outside of If statement - Laracasts
My variable can not be used outside of the if statement. Here is my controller: public function getStuff(){ if ($insert->free_meal == 1){ $push...
Read more >
How to access variable outside function in JavaScript | Code
You can't access variables declared inside a function from outside a function. The variable belongs to the function's scope only, ...
Read more >
C++ Tutorial: Static Variables and Static Class Members - 2020
Visibility: if it is defined within a function/block, it's scope is limited to the function/block. It cannot be accessed outside of the function/block....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found