Nov 9, 2012, 1:27 AM
129 Posts

Re: viewScope variable binding question

  • Category: Server Side JavaScript
  • Platform: Windows
  • Release: 8.5.3
  • Role: Developer
  • Tags:
  • Replies: 3
When you bind an object/array to a variable, you pass the reference to the object. 
viewScope.a = [1,2]; 
var b = viewScope.a; 
b.push( 3 ); 
// viewScope.a -> [1,2,3]
// b -> [1,2,3] 
If you're dealing with boolean, string or number, I believe you're safe. As you discovered, cloning the object is the way to go if you want a shallow copy of the object. You can run into issues with shallow copies (clone) as well if you're copying objects that contain other objects. 
More information about shallow/deep copies: