function map(f, a) {
for(var i in a) {
a[i] = f(a[i]);
}
}
var a = [0, 1, 2, 3];
map(function(x) { return x = x * x }, a);
console.log(a);
a: [0, 1, 4, 9]
However, If I change map(f, a) to:
function map(f, a) {
for(var i in a) {
f(a[i]);
}
}
var a = [0, 1, 2, 3];
map(function(x) { return x = x * x }, a);
console.log(a);
a remains unchanged as: [0, 1, 2, 3]
I'm not sure what's happening here. It appears as if the interpreter considers a[i] as a reference to a property of the object a in map(f, a) but once passed into f it turns into a typeof number.
var x = 0; function f(x) { x = 1 }; f(x)). You are throwing the result off(a[i])away!