This forum is closed to new posts and responses. New discussions are now taking place in the IBM Developer Answers forum.



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 

This forum is closed to new posts and responses. New discussions are now taking place in the IBM Developer Answers forum.