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.
Share Improve this question asked Apr 29, 2012 at 21:13 Mad EchetMad Echet 3,8817 gold badges31 silver badges44 bronze badges 3-
1
Exporting an API is always quite clumsy in Closure. In this case, it is better to make
myGlobalVar
a private obfuscated variable, and provide aGetMyGlobalVar
function. This way, at least you reduce the instances ofwindow["myGlobalVar"]
everywhere in your code. – Stephen Chung Commented Apr 30, 2012 at 1:59 -
Exporting your functions can be simpler if you use a global namespace object:
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 -
Thanks, I thought it over and I realized how inelegant it was to use global variables anyway. I will go for the getter and setter functions that offer much more guarantee pared to globals. I figured also that encapsulating in an
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
2 Answers
Reset to default 8New Answer
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.
Old Answer
@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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745041976a4607865.html
评论列表(0条)