FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.



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. 
 
E.g.
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: 
http://www.jusfortechies.com/java/core-java/deepcopy_and_shallowcopy.php 

FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.