这里的this我到很困惑,难道是[0 1 2 ,,]这样的么???

plglenn6 2010-01-24 07:23:03
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;
};
}

Examples
Example: Pluralizing the words (strings) in an array
The following code creates an array of "plural" forms of nouns from an array of their singular forms.

...全文
197 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2010-01-25
  • 打赏
  • 举报
回复
喔卖糕的,

this是只当前对象!

这句话是有问题的
plglenn6 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sandy945 的回复:]


https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator

还有疑问可跟帖
[/Quote]

这个链接就是我的追求啊
阿非 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 cuike519 的回复:]
this是只当前对象!
[/Quote]

说的这么铿锵有力, 你个人的理解还是文档说明?
cuike519 2010-01-25
  • 打赏
  • 举报
回复
this是只当前对象!

在你这里就是当前的数组对象,例如:

function xxx(o){
alert(o);
}

var array = ["foot", "goose", "moose"];
array.map(xxx);

上面你说的那个this就是指的array这个实例。
阿非 2010-01-25
  • 打赏
  • 举报
回复
还有一个关键的地方

就是 in

可看这里

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator

还有疑问可跟帖
阿非 2010-01-25
  • 打赏
  • 举报
回复
singles.map
-----------------

singles
->
this
阿非 2010-01-25
  • 打赏
  • 举报
回复
this

在js 中就一个意思,当前调用者。

plglenn6 2010-01-24
  • 打赏
  • 举报
回复
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;
};
plglenn6 2010-01-24
  • 打赏
  • 举报
回复
能不能用 ["foot", "goose", "moose"]; 走一下程序 !!
十八道胡同 2010-01-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 plglenn6 的回复:]
["foot", "goose", "moose"];  带入后this  指代  ["foot", "goose", "moose"];  么?
[/Quote]
对的吧
c#里this就是指本身。
js是差不多的吧
plglenn6 2010-01-24
  • 打赏
  • 举报
回复
["foot", "goose", "moose"]; 带入后this 指代 ["foot", "goose", "moose"]; 么?
wuyq11 2010-01-24
  • 打赏
  • 举报
回复
if (!Array.prototype.map) {
// 返回一个数组,现有数组中的每个元素调用提供的函数的返回值。
Array.prototype.map = function (func, context)
{
var results = [];
for (var i = 0; i < this.length; i++) {
if (i in this)
results[i] = func.call(context, this[i], i, this);
}
return results;
};
}
http://developer.51cto.com/art/200909/153026.htm
plglenn6 2010-01-24
  • 打赏
  • 举报
回复
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


["foot", "goose", "moose"]; 这个么? 1显然不在这里啊,物以类聚,这个数字与字符串不沾边
SQL77 2010-01-24
  • 打赏
  • 举报
回复
应该就是你的当前数组res

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧