我正在为Array接收一个函数,然后为Array中的每个项目调用它的数组写入扩展方法。Array上的JavaScript扩展
它工作正常,除了扩展本身被添加到函数调用后的数组。所以在警报部分,第三个警报在字符串中显示了我的foo函数。
下面的代码:
Array.prototype.foo = function (F) {
for (var i in this) {
this[i] = F(this[i]);
}
}
var items = ["a", "b"];
items.foo(function (data) {
return data + 1;
});
for (var i in items) {
alert(items[i]); // expected to see a1 and b1 but also displayed foo as string.
}
不要在''中使用''''''。 – SLaks
迭代数组内容,而不是其属性。 –
只是一个样式问题,但我不会使用大写'F'作为函数变量的名称,请使用'fn'。 – jabclab