0

I have been using underscore library for many things, however am having issues in the _.each function.

Documentation is here: http://documentcloud.github.com/underscore/#each

If you see this; http://jsfiddle.net/52nrV/, you will notice that first _.each function changes the values of the data object. Am I doing something wrong, this shouldn't be an expected behaviour I guess. What is the problem in my code?

Thanks,

Merinn
  • 127
  • 1
  • 9

1 Answers1

0

Objects are assigned by reference in JavaScript, they are not copied. Example:

var a = {foo: 42};
var b = a;
a === b; // true
// both a and b reference the same object

var a = {foo: 42};
var b = {foo: 42};
a === b; // false
// a and b look similar but are different objects

Have a look at this question if you want to clone or copy an object.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143