I'm writing an extension method for Array which accepts a function and then call it for each items in the Array.
It works fine except the extension itself is added into the Array after the function call. So at the alert part, the third alert shows my foo function in string.
Here's the code:
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.
}
Ffor the function variable's name, go for something likefn.