Skip to content

Defining fuctions within the Script

pranithcodes edited this page Apr 6, 2018 · 1 revision

We can also define local script function by using function keyword.

Below is the script to define the function within the script:

var isDivisibleBySix = function(number) { if(number%6 == 0 ){ out.println(number + " is divided by 6") }else{ out.println(number + " is not divided by 6") } };

In order to call the function within the script then we have use small brackets with the parameters. For example:

isDivisibleBySix(30); isDivisibleBySix(26);

public static void main(String[] args) throws ScriptException {

String script = "var isDivisibleBySix = function(number) " + "{ if(number%6 == 0 )" + " {out.println(number + " is divided by 6")}" + "else" + " {out.println(number + " is not divided by 6")}" + "};" + " isDivisibleBySix(30); " + " isDivisibleBySix(26);";

	JexlScriptEngine jexlScriptEngine = new JexlScriptEngine();

	Bindings bindings = new SimpleBindings();
	bindings.put("out", System.out);

	jexlScriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

	jexlScriptEngine.eval(script);

}

Clone this wiki locally