如何在rhino javascript中将对象属性添加到全局对象

时间:2022-06-24 21:04:08

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:

我在一个对象中有一些属性,我想添加到全局命名空间。在浏览器的javascript中,我可以将它添加到窗口对象,如下所示:

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?

但由于rhino没有全局窗口对象,我无法做到这一点。有没有一个等效的对象或其他方式来实现这一目标?

5 个解决方案

#1


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

您可以使用this,如果当前函数未被调用为对象的方法,则引用全局对象。

#2


I found a rather brilliant solution at NCZOnline:

我在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()或apply()并将null作为第一个参数传入,此对象始终指向全局对象。由于null作用域无效,因此解释器会插入全局对象。该函数使用内部函数来确保范围始终正确。

Call using:

var glob = getGlobal();

glob will then return [object global] in Rhino.

然后glob会在Rhino中返回[object global]。

#3


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);

#4


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

另请参阅:我可以为Java6 Rhino脚本引擎中运行的javascript创建“窗口”对象

#5


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

我没有使用过rhino,但你不能只使用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.

编辑:该死的知道会有一个问题!迈尔斯的建议就是那种情况。

#1


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

您可以使用this,如果当前函数未被调用为对象的方法,则引用全局对象。

#2


I found a rather brilliant solution at NCZOnline:

我在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()或apply()并将null作为第一个参数传入,此对象始终指向全局对象。由于null作用域无效,因此解释器会插入全局对象。该函数使用内部函数来确保范围始终正确。

Call using:

var glob = getGlobal();

glob will then return [object global] in Rhino.

然后glob会在Rhino中返回[object global]。

#3


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);

#4


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

另请参阅:我可以为Java6 Rhino脚本引擎中运行的javascript创建“窗口”对象

#5


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

我没有使用过rhino,但你不能只使用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.

编辑:该死的知道会有一个问题!迈尔斯的建议就是那种情况。