为什么我不能使用Array.filter()
在犀牛?
代码是这样的:
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);
两种情况都会输出“未定义”。
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 https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter gives you an compatibility 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;
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)