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

Why can't I use Array.filter() in Rhino?The code is like this:var simple_reason = ["a",

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744923390a4601276.html

相关推荐

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

    Why can't I use Array.filter() in Rhino?The code is like this:var simple_reason = ["a",

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信