Array.filter 服务器端函数 Javascript

Array.filter function in serverside Javascript

我想使用服务器端 Javascript 在我的 XPage 中过滤一个数组。不幸的是我收到以下错误:

Error calling method 'filter(Function)' on an object of type 'Array [JavaScript Object]'

我有一个字符串数组,例如 ["elem1","elem2","elem3"]

我这样调用函数:

list.filter(function(){

});

是否有任何原因导致此错误发生? ssjs中有这个功能吗?

这个问题不重复,因为不清楚 Xpages/Lotus Notes 在后台运行 Rhino。

听起来您使用的任何服务器端 JavaScript 环境都不支持 ES5 功能(这是 2009 年 12 月第 5 版规范中的功能)。

您可以为此使用 polyfill 以及添加到 Array 的其他内容,请参阅 MDN, but beware: If ES5 features aren't supported, it's impossible to add things to Array.prototype without making them enumerable, meaning any code (mis)using for-in to loop through arrays 会受到影响。

XPages 背后有 Rhino。而这个 SO 主题 No Array.filter() in Rhino? 说,它已经过时了。

编辑: 不它不是。几年前,我在某处读到过它。现在看来并非如此。根据 Dan Sickles 的评论(引用 Philippe Riand?)here

It runs on the server jvm and uses javascript as the application language. For licensing reasons, IBM wrote their own jvm javascript engine instead of using Rhino. With Rhino shipping in java 6, they should be able to ship it in Designer 8.5 (or later). The licensing problems may have been around the extensions like @Formulas and type declarations. Classes, modules/namespaces and type declarations are coming in javascript 2 and even google is helping to get that implemented in Rhino. I'd hate to see a non-standard, javascript engine underlying the coolest web development technology in Domino.

Speaking of Rhino, the "most important new feature that is not as certain to be in 8.5 as XPages" uses Rhino and other jvm scripting languages on the client. If this makes it into the product, two years from now most new Notes applications will be written in neither Lotusscript nor Java. I'll leave it at that.

事实上,关于如何在 XPages 中使用 Rhino 的主题很少,因此使用最新的 Rhino 版本您的代码可以工作。无论如何,我的建议是使用 Java 调用。