No Array.filter () in Rhino?

Why can't I use Array.filter()in Rhino?

The code is as follows:

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 pins output "undefined".

+3
source share
3 answers

There is no standardized function filterfor Javascript arrays, this is just an extension of the standard. (There is an ES5 specification published just a month after posting this answer.) The MDC man page gives you a compatibility pattern for those implementations that don't 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;
  };
}
+4
source

Rhino, JavaScript 1.6. Rhino 1.7.

+4

Is the filter standard javascript? This is only in Mozilla with 1.8 (or this link tells me)

+1
source

Source: https://habr.com/ru/post/1723911/


All Articles