ScriptEngine JavaScript不';t支持包括?

ScriptEngine JavaScript不';t支持包括?,javascript,java,nashorn,javax,Javascript,Java,Nashorn,Javax,我有如下代码 ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); engine.eval("[1, 2, 3].includes(1)"); 但这会引发以下错误 javax.script.ScriptException: TypeError: [1, 2, 3].includes is not a function i

我有如下代码

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
engine.eval("[1, 2, 3].includes(1)");
但这会引发以下错误

javax.script.ScriptException: TypeError: [1, 2, 3].includes is not a function in <eval> at line number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
javax.script.ScriptException:TypeError:[1,2,3]。includes不是第1行的函数
位于jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
位于jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454)
位于jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
位于jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
位于jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
位于javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)

我可以改为使用indexOf(1),这似乎是可行的,但是有没有理由我不能使用这个解析器访问includes?

String.prototype.includes在ECMAScript 2015(ECMA-262第6版)中指定。Nashorn引擎实现ECMA-262版本5.1)。另见


具有String.prototype.includes的polyfill。我检查了polyfill是否与Nashorn引擎一起工作。

Nashorn不支持includes(),因为它在添加includes()之前实现了早期版本的JavaScript规范。您可以使用Mozilla网站上的参考资料,将对includes()的polyfill(也称为shim)支持添加到Nashorn中

  • 字符串包含()多边形填充:
  • 数组包含()多边形填充:
最初的问题是在JavaScript数组上执行includes()。之前的答案是针对JavaScript字符串的,因此不正确。下面是一段JUnit测试代码,展示了如何对数组和字符串使用polyfills

    // Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
    public static final String NASHORN_POLYFILL_STRING_PROTOTYPE_INCLUDES = "if (!String.prototype.includes) { Object.defineProperty(String.prototype, 'includes', { value: function(search, start) { if (typeof start !== 'number') { start = 0 } if (start + search.length > this.length) { return false } else { return this.indexOf(search, start) !== -1 } } }) }";
    // Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
    public static final String NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES  = "if (!Array.prototype.includes) { Object.defineProperty(Array.prototype, 'includes', { value: function(valueToFind, fromIndex) { if (this == null) { throw new TypeError('\"this\" is null or not defined'); } var o = Object(this); var len = o.length >>> 0; if (len === 0) { return false; } var n = fromIndex | 0; var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); function sameValueZero(x, y) { return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); } while (k < len) { if (sameValueZero(o[k], valueToFind)) { return true; } k++; } return false; } }); }";

    @Test
    public void testStringIncludesWithPolyfill() throws Exception {
        runScript(NASHORN_POLYFILL_STRING_PROTOTYPE_INCLUDES, "'[1, 2, 3]'.includes(2)");
    }

    @Test(expected=javax.script.ScriptException.class)
    public void testStringIncludesWithoutPolyfill() throws Exception {
        runScript(null, "'[1, 2, 3]'.includes(2)");
    }

    @Test
    public void testArrayIncludesWithPolyfill() throws Exception {
        runScript(NASHORN_POLYFILL_ARRAY_PROTOTYPE_INCLUDES, "[1, 2, 3].includes(2)");
    }

    @Test(expected=javax.script.ScriptException.class)
    public void testArrayIncludesWithoutPolyfill() throws Exception {
        runScript(null, "[1, 2, 3].includes(2)");
    }

    private void runScript(final String polyfill, final String booleanExpression) throws ScriptException {
        final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        final ScriptEngine        scriptEngine        = scriptEngineManager.getEngineByName("nashorn");
        Assert.assertNotNull(scriptEngine);
        if (null != polyfill) {
            scriptEngine.eval(polyfill);
        }
        final Object booleanExpressionResult = scriptEngine.eval(booleanExpression);    // returns Boolean object
        Assert.assertNotNull(booleanExpressionResult);
        Assert.assertEquals(booleanExpressionResult.getClass(), Boolean.class);
        System.out.println(booleanExpression + " = " + booleanExpressionResult.toString());
    }
//复制自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
公共静态最终字符串NASHORN_POLYFILL_String_PROTOTYPE_INCLUDES=“if(!String.PROTOTYPE.INCLUDES){Object.defineProperty(String.PROTOTYPE,'INCLUDES',{value:function(search,start){if(typeof start!='number'){start=0}if(start+search.length>this.length){return false}否则{返回this.indexOf(search,start)!=-1} }) }";
//抄袭https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
公共静态最终字符串NASHORN\u POLYFILL\u ARRAY\u PROTOTYPE\u INCLUDES=“if(!ARRAY.PROTOTYPE.INCLUDES){Object.defineProperty(ARRAY.PROTOTYPE,'INCLUDES',{value:function(valueToFind,fromIndex){if(this==null){throw new TypeError('\'this\'为null或未定义');}var o=Object this;var len len o=o.length>>0;if(len==0){return false;}var n=fromIndex | 0;var k=Math.max(n>=0?n:len-Math.abs(n,0);函数sameValueZero(x,y){return x==y | |(typeof x==number'&typeof y==number'&isNaN(x)&isNaN(y))}而(k
因为
includes
来自ecmascript 2016?您知道是否有一个Java兼容的JavaScript解析器将包含ecmascript 2016吗?我如何使用您在我的示例中引用的polyfill?在评估使用String.prototype.includes函数的代码之前,从该页面“评估”该polyfill代码。