Local functions are wrongly made global in Strict mode
See original GitHub issueWhen strict mode is enabled, the following code triggers an error for the second call to func1
:
'use strict';
{
function func1(arg){ //func1 is local to the scope, not global
alert(arg) ;
}
func1('Hi') ;
}
func1('Hi') ; // <-- Error: func1 does not exist globally
When we compile that code:
java -jar closure-compiler-v20190121.jar --language_out NO_TRANSPILE -O SIMPLE --js_output_file Output.js Input.js
We get:
'use strict';
var func1 = function(a) {
alert(a);
};
func1("Hi");
func1("Hi");
i.e. func1
was converted to a global function, which makes the code behave different than the original.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Strict mode - JavaScript - MDN Web Docs
JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode".
Read more >Why does deleting global and deleting var result in different ...
In node.js all your code is inside a module function so anything you declare with var is NOT a global. It's a local...
Read more >Global app variables under "use strict" · Issue #1380 - GitHub
It seems it is impossible to define global app variables when running code under "use strict". In documentation it is written: Notice that...
Read more >20 Reasons Why You Need To Stop Being Highly Obsessed ...
Unnecessary Strict Mode Errors Flagged by ESLint Beforehand. 4. Implied Globals. First, strict mode makes it impossible to accidentally create global variables.
Read more >Using Strict Mode
function sum(a, a, c) { // !!! syntax error 'use strict'; return a + a + c; // wrong if this code ran...
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
There’s no solution so far, but there is a workaround.
Instead of this:
Do this:
In the first case,
func
may be global or local depending on whether strict mode is in effect or not. And that’s probably the reason why Closure Compiler gets it wrong.Whereas in the second case,
func
is always local, so Closure Compiler doesn’t get confused.Any news about this issue? Did you find a solution?