Tom
Tom

Reputation: 4826

How to declare global variable for google closure compiler?

(function(){ window.myGlobal=42})();
console.error(myGlobal);

java -jar compiler.jar --jscomp_warning undefinedVars

WARNING - variable myGlobal is undeclared

Upvotes: 2

Views: 3166

Answers (1)

Tyler
Tyler

Reputation: 22116

As Rohan points out in the comments, the Closure compiler thinks of window.myGlobal and myGlobal as different things, even though you and I know they are actually the same. If you need to define it inside a function, you could do something like

var myGlobal;
(function() { myGlobal = 42; })();
console.log(myGlobal);

or, use window in all cases:

window.myGlobal = null;
(function() { window.myGlobal = 42; })();
console.log(window.myGlobal);

Upvotes: 2

Related Questions