Function defined inside conditionals can't be accessed outside
See original GitHub issueWhat ?
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:
Gulp task
My attempt
- the above is fixed if I define the insideFunc like this
var insideFunc = function() {
};
- 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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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
is like you had written
so the
insideFunc
variable only exists within the scope of theif
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