8

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?

timdisney
  • 5,027
  • 9
  • 33
  • 30
  • I figured out how to do it without having to write extra javascript: https://stackoverflow.com/a/60783337/473201 – phreakhead Mar 21 '20 at 00:35

5 Answers5

11

I found a rather brilliant solution at NCZOnline:

function getGlobal(){
  return (function(){
    return this;
    }).call(null);
}

The key to this function is that the this object always points to the global object anytime you are using call() or apply() and pass in null as the first argument. Since a null scope is not valid, the interpreter inserts the global object. The function uses an inner function to assure that the scope is always correct.

Call using:

var glob = getGlobal();

glob will then return [object global] in Rhino.

Pacerier
  • 76,400
  • 86
  • 326
  • 602
pnewhook
  • 3,971
  • 1
  • 27
  • 49
  • Brilliant! Remember that this will not work in ES5 strict mode, so your script may fail once Rhino [introduces ES5-conformance](https://bugzilla.mozilla.org/show_bug.cgi?id=517860). – user123444555621 Sep 27 '10 at 08:46
  • 1
    cool thanks. `function getGlobal(){return (function(){return this})()}` is simpler and equivalent (confirmed by the author) – mykhal Jul 29 '11 at 15:47
  • @user123444555621, Why wouldn't this work in ES5 strict mode? – Pacerier Sep 18 '15 at 05:07
  • It will return `null` instead: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode/Transitioning_to_strict_mode#this_in_function_calls – Miles Sep 18 '15 at 23:50
  • Jsfiddle showing that this [does not work in ES5 strict mode](https://jsfiddle.net/mendesjuan/tses04da/) even if `getGlobal` itself doesn't have "use strict" (but is within another function that is marked as "use strict" – Juan Mendes Feb 25 '16 at 13:51
7

You could use this, which refers to the global object if the current function is not called as a method of an object.

Miles
  • 28,169
  • 7
  • 57
  • 71
  • 2
    var window = this; at the very beginning of the script helped me out. See the env.js script by John Resig http://ejohn.org/blog/bringing-the-browser-to-the-server/ – GrGr Aug 26 '09 at 08:28
  • @Miles, Hmm, don't you think that pnewhook's solution is better? – Pacerier Sep 18 '15 at 05:03
5

Here's how I've done it in the past:

// Rhino setup
Context jsContext = Context.enter();
Scriptable globalScope = jsContext.initStandardObjects();

// Define global variable
Object globalVarValue = "my value";
globalScope.put("globalVarName", globalScope, globalVarValue);
edsoverflow
  • 591
  • 3
  • 3
1

You could just define your own window object as a top-level variable:

var window = {};

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

Community
  • 1
  • 1
harto
  • 84,815
  • 7
  • 43
  • 60
-1

I've not used rhino but couldn't you just use var?

i.e.


var foo = myObject.foo;
foo();

Edit: Damn knew there'd be a catch! Miles' suggestion would be the go in that case.

rezzif
  • 460
  • 2
  • 10