Re: [jQuery] Use $.extend to clone object

2006-09-27 Thread Michael Geary
 From: Klaus Hartl
  
  is it possible to use $.extend to clone an object?
  
  var template = { ... };
  
  clone = $.extend({}, template);
 
 To answer my question, yes it works :-)

Keep in mind that extend() does a shallow copy, not a deep copy.

   var one = { a:1, b:{ c:2 } };
   var two = $.extend( {}, one );
   two.a = 11;
   two.b.c = 33;

Now the objects look like this:

   one = { a:1, b:{ c:33 } }
   two = { a:11, b:{ c:33 } }

The two.b.c = 33; assignment changed both objects, because they share a
copy of the b object.

-Mike


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Use $.extend to clone object

2006-09-27 Thread Klaus Hartl


Michael Geary schrieb:
 From: Klaus Hartl
 is it possible to use $.extend to clone an object?

 var template = { ... };

 clone = $.extend({}, template);
 To answer my question, yes it works :-)
 
 Keep in mind that extend() does a shallow copy, not a deep copy.
 
var one = { a:1, b:{ c:2 } };
var two = $.extend( {}, one );
two.a = 11;
two.b.c = 33;
 
 Now the objects look like this:
 
one = { a:1, b:{ c:33 } }
two = { a:11, b:{ c:33 } }
 
 The two.b.c = 33; assignment changed both objects, because they share a
 copy of the b object.
 
 -Mike


Thank you Mike! It works for what I needed but your hint saved me from 
trouble in the future...


-- Klaus

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/