My webapp is based on a mon script where I define the mon functions and a global variable and dynamically loaded scripts that process those. So far, the only way I found to export the global variable is to replace any occurrence by window["myGlobalVar"]
but I find it very ugly. Is there a better way to do?
Here is an illustration
// monscript.js before pilation
function incrementVariable() {window["myGlobalVar"]++;}
window["incrementVariable"] = incrementVariable;
window["myGlobalVar"] = 0;
and in another script
alert(myGlobalVar); // <= alerts 0
incrementVariable();
alert(myGlobalVar); // <= alerts 1
I am looking for a way to use directly myGlobalVar
in both files because it would be more elegant. However, I would need to set window["myGlobalVar"]
to a pointer and not a copy of the object and I am not sure how to do that on simple types.
Is it possible? Is encapsulating myGlobalVar
in an Object
the only other way?
Thanks a lot for your lights.
My webapp is based on a mon script where I define the mon functions and a global variable and dynamically loaded scripts that process those. So far, the only way I found to export the global variable is to replace any occurrence by window["myGlobalVar"]
but I find it very ugly. Is there a better way to do?
Here is an illustration
// monscript.js before pilation
function incrementVariable() {window["myGlobalVar"]++;}
window["incrementVariable"] = incrementVariable;
window["myGlobalVar"] = 0;
and in another script
alert(myGlobalVar); // <= alerts 0
incrementVariable();
alert(myGlobalVar); // <= alerts 1
I am looking for a way to use directly myGlobalVar
in both files because it would be more elegant. However, I would need to set window["myGlobalVar"]
to a pointer and not a copy of the object and I am not sure how to do that on simple types.
Is it possible? Is encapsulating myGlobalVar
in an Object
the only other way?
Thanks a lot for your lights.
myGlobalVar
a private obfuscated variable, and provide a GetMyGlobalVar
function. This way, at least you reduce the instances of window["myGlobalVar"]
everywhere in your code.
– Stephen Chung
Commented
Apr 30, 2012 at 1:59
window["mynamespace"] = { "incrementVariable":incrementVariable, "getMyGlobalVar":function() { return myGlobalVar }};
Then you can do: alert(mynamespace.getMyGlobalVar()); mynamespace.incrementVariable(); ...
– Stephen Chung
Commented
Apr 30, 2012 at 2:00
Object
was not so great either. I would have the same issue with the object members which names would be obfuscated. I'd still need getter and setter.
– Mad Echet
Commented
Apr 30, 2012 at 8:34
Closure-piler supports an @nocollapse
annotation which prevents a property from being collapsed to a global variable. This allows the property to be mutable when exported.
@nocollapse
does not block renaming - you still need to export a property to acplish that.
@nocollapse
is currently only supported when piling from source. It will be included in the next release - that is versions AFTER the v20150315 release.
@expose
is now deprecated. The piler will warn about any usage of @expose
There is a new, but so far undocumented, annoatation: @expose. This single annotation will both export a property and prevent it from being collapsed off a constructor. It sounds like the perfect fit for your situation - but it will require your variable to be a property on an object.
However, use with care. Any properties which have @expose will not be renamed and will not be removed as dead code. This makes it especially problematic for use by javascript library writers.
If you want to have a variable that doesn't get renamed, just create a file called for example props.txt
with the following contents:
myGlobalVar:myGlobalVar
Then when piling your code, add the mand line argument: --property_map_input_file props.txt
Your variable will not be renamed and is available to all scripts as long as it's not optimized away. Also, if you don't declare it at all (so you omit var myGlobalVar
), it won't get renamed or removed.