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
3 Answers
Reset to default 4There is no standardized (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...filter
function for Javascript Arrays, it is only an extension to the standard.
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)