Skip to content Skip to sidebar Skip to footer

Nashorn: Call Function Inside Of A Namespace

I have evaluated the following script using the NashornScriptEngine: var Namespace = { test: function() { return 'It works'; } } Now I want to call the functio

Solution 1:

You are trying to access a global function called window["Namespace.test"], not window.Namespace.Test. You first need to get a reference to Namespace, then you can call invocable.invokeMethod specifying Namespace as its context (this).

For example, to call JSON.parse(), you can use the following:

Object json = engine.eval("JSON"); // Or "Namespace" in your caseObject data = invocable.invokeMethod(json, "parse", contactJson); //"test" for the case you mention

Post a Comment for "Nashorn: Call Function Inside Of A Namespace"