繁体   English   中英

在Rhino中没有Array.filter()?

[英]No Array.filter() in Rhino?

为什么我不能在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);

两种情况都输出“未定义”。

Javascript Arrays没有标准化的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;
  };
}

您使用的是过时版本的Rhino,它没有实现JavaScript 1.6。 试试Rhino 1.7

是过滤器标准的JavaScript吗? 从1.8开始它只在Mozilla中(或者这个引用告诉我)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM