How can I add an object property to the global object in rhino javascript
Asked Answered
G

5

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?

Giacobo answered 22/7, 2009 at 4:1 Comment(1)
I figured out how to do it without having to write extra javascript: https://mcmap.net/q/907574/-how-do-i-call-a-method-of-a-java-instance-from-javascriptLohrman
H
7

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

Hotbox answered 22/7, 2009 at 4:14 Comment(2)
var window = this; at the very beginning of the script helped me out. See the env.js script by John Resig ejohn.org/blog/bringing-the-browser-to-the-serverFugue
@Miles, Hmm, don't you think that pnewhook's solution is better?Hornbeck
T
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.

Taxis answered 23/3, 2010 at 18:59 Comment(5)
Brilliant! Remember that this will not work in ES5 strict mode, so your script may fail once Rhino introduces ES5-conformance.Nimrod
cool thanks. function getGlobal(){return (function(){return this})()} is simpler and equivalent (confirmed by the author)Woadwaxen
@user123444555621, Why wouldn't this work in ES5 strict mode?Hornbeck
It will return null instead: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Hotbox
Jsfiddle showing that this does not work in ES5 strict mode even if getGlobal itself doesn't have "use strict" (but is within another function that is marked as "use strict"Rheinlander
H
7

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

Hotbox answered 22/7, 2009 at 4:14 Comment(2)
var window = this; at the very beginning of the script helped me out. See the env.js script by John Resig ejohn.org/blog/bringing-the-browser-to-the-serverFugue
@Miles, Hmm, don't you think that pnewhook's solution is better?Hornbeck
T
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);
Tuneberg answered 22/7, 2009 at 4:33 Comment(1)
Why not simply use this?Hornbeck
R
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

Reprove answered 22/7, 2009 at 4:17 Comment(0)
S
-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.

Superfluity answered 22/7, 2009 at 4:5 Comment(3)
Hah! Too simple for me :) Thanks!Giacobo
Ah, wait...here's my real problem. I want to do this programmaticaly. I don't actually know the names of the object properties before hand.Giacobo
This answer is unfortunately just clutter, as such could it be deleted? No hard feelings!Friulian

© 2022 - 2024 — McMap. All rights reserved.