When you write var fakeUser = user
you copy reference to the object user - fakeUser
is still referencing the same object.
If you want copy the object, you have to clone it or copy only desired properties of the user: e.g.:
var fakeUser = {
name: user.name,
id: user.id
}
JS clonning is described in following answers:
answer 1
answer 2
You can also use a clone method of underscore library (But be aware that it does just a shallow copy (not deep) - i.e. if user object contains some references, they are copied as references).
Simple method to clone object is to just serialize and deserilaze it (but this will not work on circular references - when object is referencing itself or it's part):
function clone(a) {
return JSON.parse(JSON.stringify(a));
}