I'm using javax.script to execute javascript from a java method.
In my java method I invoke different functions defined in javascript. On the javascript side I want to keep a global variable so the output of a call depends on the previous ones.
java method
public void myMethod(){
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval(new java.io.FileReader("myTest.js"));
Invocable inv = (Invocable) engine;
Object obj = engine.get("obj");
inv.invokeMethod(obj, "method1");
inv.invokeMethod(obj, "method2");
}
myTest.js
var obj=new Object();
var myStatus=1;
obj.method1 = function(){
myStatus++;
};
obj.method2 = function(){
for (var i=0; i<myStatus)
println('Hello world');
}
What is the scope of the variable declared in the script? If I add a global variable to the script using
engine.put("globalVariable", myVariable)
what is the scope of this variable?
Thanks