最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - No Array.filter() in Rhino? - Stack Overflow

programmeradmin2浏览0评论

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".

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".

Share asked Nov 25, 2009 at 16:48 Kuroki KazeKuroki Kaze 8,4924 gold badges38 silver badges49 bronze badges 4
  • What version of Rhino are you using? When I run it in 1.7 I get "function" for both cases (they are exactly equivalent by the way, unless you change Array). – Matthew Crumley Commented Nov 25, 2009 at 19:49
  • When you start it interactively (i.e. without a file to run) it should print the version when it starts. – Matthew Crumley Commented Nov 26, 2009 at 23:29
  • Rhino 1.5 release 4 2003 01 16 - pretty old – Kuroki Kaze Commented Dec 1, 2009 at 16:46
  • I'll update it now, hopefully get speed boost also :) – Kuroki Kaze Commented Dec 1, 2009 at 16:47
Add a ment  | 

3 Answers 3

Reset to default 4

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 patibility 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;
  };
}

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

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

发布评论

评论列表(0)

  1. 暂无评论