62,046
社区成员
发帖
与我相关
我的任务
分享
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this) //这里的this我到很困惑,难道是[0 1 2 ,,]这样的么???
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
Array.prototype.map = function(fun /*, thisp*/) // ["foot", "goose", "moose"];
{
var len = this.length >>> 0; //len=3
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this) //这里的this我到很困惑,难道是[0 1 2 ,,]这样的么???
res[i] = fun.call(thisp, this[i], i, this);
//i=0 in "foot", "goose", "moose"]; 这里很无语啊
}
return res;
};
function makePseudoPlural(single)
{
return single.replace(/o/g, "e");
}
var singles = ["foot", "goose", "moose"];
var plurals = singles.map(makePseudoPlural);
// plurals is ["feet", "geese", "meese"]
// singles is unchanged