Rhino 中没有 Array.filter() 吗?

发布于 2024-08-13 02:31:12 字数 292 浏览 5 评论 0原文

为什么我不能在 Rhino 中使用Array.filter()

代码是这样的:

var simple_reason = ["a", "b", "c"];
print(typeof simple_reason.filter);

var not_so_simple_reason = new Array("a", "b", "c");
print(typeof not_so_simple_reason.filter);

两种情况都输出“undefined”。

Why can't I use Array.filter() in Rhino?

The code is like this:

var simple_reason = ["a", "b", "c"];
print(typeof simple_reason.filter);

var not_so_simple_reason = new Array("a", "b", "c");
print(typeof not_so_simple_reason.filter);

Both cases output "undefined".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无法回应 2024-08-20 02:31:12

Javascript 数组没有标准化的 filter 函数,它只是标准的扩展。 (ES5 规范在此后一个月发布)答案已发布。) MDC 参考页面 为您提供了一个兼容性示例,用于那些不支持它的实现...

if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

There is no standardized filter function for Javascript Arrays, it is only an extension to the standard. (There is as of the ES5 spec published just a month after this answer was posted.) The MDC reference page gives you an compatibility sample to use for those implementations that do not support it...

if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}
烟雨扶苏 2024-08-20 02:31:12

您正在使用不实现 JavaScript 1.6 的过时版本的 Rhino。尝试Rhino 1.7

You are using an outdated version of Rhino that does not implement JavaScript 1.6. Try Rhino 1.7.

半边脸i 2024-08-20 02:31:12

过滤器是标准的javascript吗?自 1.8 起,它仅在 Mozilla 中出现(或者 此参考 告诉我)

Is filter standard javascript? It is only in Mozilla since 1.8 (or so this reference tells me)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文