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

后端 未结 5 1875
迷失自我
迷失自我 2021-02-15 18:48

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:

5条回答
  •  渐次进展
    2021-02-15 19:12

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

提交回复
热议问题