See the examples below:
function map_cloning(obj,fn){
result = {};
for (var key in obj)
result[key] = fn(obj[key]);
return result;
};
function map_modifying(obj,fn){
for (var key in obj)
obj[key] = fn(obj[key]);
return obj;
};
function sum_cloning(vector1,vector2){
return [vector1[0] + vector1[1], vector2[0] + vector2[1]];
};
function sum_modifying(vector1,vector2){
vector1.x += vector2.x;
vector1.y += vector2.x;
return vector1;
};
function tail_cloning(obj){
return obj.slice(1);
};
function tail_modifying(obj){
obj.splice(0,1);
return obj;
};
Often you have to chose between cloning or not an object before making a computation. Which is best?