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.

Local functions are wrongly made global in Strict mode

See original GitHub issue

When 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:open
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Getfreecommented, Mar 29, 2019

There’s no solution so far, but there is a workaround.

Instead of this:

{
    function func(){ . . . }
}

Do this:

{
    let func = function(){ . . . } ;
}

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.

0reactions
devalnorcommented, Mar 29, 2019

Any news about this issue? Did you find a solution?

Read more comments on GitHub >

github_iconTop 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 >

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