I have some properties in an object that I would like to add to the global namespace. In javascript on the browser I could just add it to the window
object like so:
var myObject = {
foo : function() {
alert("hi");
}
// and many more properties
};
for (property in myObject) {
window[property] = myObject[property];
}
// now I can just call foo()
foo();
But since rhino doesn't have the global window object I can't do that. Is there an equivalent object or some other way to accomplish this?
I found a rather brilliant solution at NCZOnline:
Call using:
glob
will then return[object global]
in Rhino.Here's how I've done it in the past:
You could just define your own
window
object as a top-level variable:You can then assign values to it as you please. ("
window
" probably isn't the best variable name in this situation, though.)See also: Can I create a 'window' object for javascript running in the Java6 Rhino Script Engine
I've not used rhino but couldn't you just use var?
i.e.
Edit: Damn knew there'd be a catch! Miles' suggestion would be the go in that case.
You could use
this
, which refers to the global object if the current function is not called as a method of an object.