简体   繁体   English

在Rhino中没有Array.filter()?

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

Why can't I use Array.filter() in Rhino? 为什么我不能在Rhino中使用Array.filter()

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". 两种情况都输出“未定义”。

There is no standardized filter function for Javascript Arrays, it is only an extension to the standard. Javascript Arrays没有标准化的filter功能,它只是标准的扩展。 (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... (在发布此答案后的一个月内发布了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;
  };
}

You are using an outdated version of Rhino that does not implement JavaScript 1.6. 您使用的是过时版本的Rhino,它没有实现JavaScript 1.6。 Try Rhino 1.7 . 试试Rhino 1.7

Is filter standard javascript? 是过滤器标准的JavaScript吗? It is only in Mozilla since 1.8 (or so this reference tells me) 从1.8开始它只在Mozilla中(或者这个引用告诉我)

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

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