在Kotlin中 使用js 函数
import javax.script.Invocable import javax.script.ScriptEngineManager fun main(args: Array<String>) { val engine = ScriptEngineManager().getEngineByName("nashorn") val js = """ function rebate(price, sale_count) { if(sale_count > 30) return price * 10 / 100; if(sale_count > 20) return price * 20 / 100; return price * 30 / 100; } """ engine.eval(js) val invocable = engine as Invocable val res = invocable.invokeFunction("rebate", 100, 200) println(res) }