简体   繁体   English

是否有可能使Rhino为javascript方法保留变量

[英]is it possible to make Rhino keep a variable for a javascript method

The context is the following : I must write a javascript method that will be executed by a java application using Rhino. 上下文如下:我必须编写一个JavaScript方法,该方法将由使用Rhino的Java应用程序执行。 I cannot make any changes to the java application. 我无法对Java应用程序进行任何更改。

Then the question is: Would it be possible to ask Rhino to save "something" in its execution context and make it available to the javascript method for next executions of this javascript method ? 那么问题是:是否可以要求Rhino在其执行上下文中保存“某物”,并将其提供给javascript方法以供该javascript方法的下一次执行使用? and then, if yes, could this "something" be an instance of a class created on the fly by the javascript method because the java application does not contain any class that could fulfill my needs. 然后,如果是,则此“内容”可以是由javascript方法动态创建的类的实例,因为Java应用程序不包含任何可以满足我的需求的类。

The short answer is no. 最简洁的答案是不。 Rhino doesn't have any global variables that you can use. Rhino没有可以使用的任何全局变量。

You will have to change the code of the Java application and add a new global variable to the execution context of Rhino. 您将不得不更改Java应用程序的代码,并将新的全局变量添加到Rhino的执行上下文中。 The Java code then needs to remember the value of the variable somewhere when the script is finished and put it again into the context when the scripts should run again. 然后,Java代码需要在脚本完成时在某处记住变量的值,并在脚本应再次运行时再次将其放入上下文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM