ScriptEngine JavaScript 不支持包含?

ScriptEngine JavaScript Doesn't Support Contains?

我有一些代码如下。

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval("[1, 2, 3].contains(1)");

但是会抛出以下错误

javax.script.ScriptException: TypeError: [1, 2, 3].contains is not a function in <eval> at line number 1

我可以改用 indexOf(1),这似乎可行,但我无法使用此解析器访问 contains 有什么原因吗?

您正在寻找 Array.prototype.includes

您的代码将是:

engine.eval("[1, 2, 3].includes(1)");