<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Deep copying of Objects and Arrays</title>
	<atom:link href="http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 29 Aug 2010 12:37:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Luke</title>
		<link>http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/comment-page-1/#comment-10679</link>
		<dc:creator>Luke</dc:creator>
		<pubDate>Mon, 27 Apr 2009 18:15:50 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=771#comment-10679</guid>
		<description>Unfortunately, deep copying a complex structure can be much more complicated than this, depending on the level of uniqueness and precision you need from the original vs the copy.  A few examples: objects storing DOM elements, maintaining prototype relationships to protect instanceof, and using functions as namespaces.

On a more focused note, the final function doesn&#039;t account for null, which is (sadly) typeof &#039;object&#039;.  Also, you can use for..in for both arrays and objects.

&lt;pre lang=&quot;javascript&quot;&gt;
function deepCopy(o) {
    var copy = o,k;

    if (o &amp;&amp; typeof o === &#039;object&#039;) {
        copy = Object.prototype.toString.call(o) === &#039;[object Array]&#039; ? [] : {};
        for (k in o) {
            copy[k] = deepCopy(o[k]);
        }
    }

    return copy;
}
&lt;/pre&gt;

This will handle sparse arrays and cases of arrays treated as objects (e.g. var a = []; a.dontDoThis = &#039;you should use an Object&#039;)</description>
		<content:encoded><![CDATA[<p>Unfortunately, deep copying a complex structure can be much more complicated than this, depending on the level of uniqueness and precision you need from the original vs the copy.  A few examples: objects storing DOM elements, maintaining prototype relationships to protect instanceof, and using functions as namespaces.</p>
<p>On a more focused note, the final function doesn&#8217;t account for null, which is (sadly) typeof &#8216;object&#8217;.  Also, you can use for..in for both arrays and objects.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> deepCopy<span class="br0">&#40;</span>o<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw2">var</span> copy <span class="sy0">=</span> o<span class="sy0">,</span>k<span class="sy0">;</span>
&nbsp;
    <span class="kw1">if</span> <span class="br0">&#40;</span>o <span class="sy0">&amp;&amp;</span> <span class="kw1">typeof</span> o <span class="sy0">===</span> <span class="st0">'object'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        copy <span class="sy0">=</span> Object.<span class="me1">prototype</span>.<span class="me1">toString</span>.<span class="me1">call</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span> <span class="sy0">===</span> <span class="st0">'[object Array]'</span> <span class="sy0">?</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">:</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>
        <span class="kw1">for</span> <span class="br0">&#40;</span>k <span class="kw1">in</span> o<span class="br0">&#41;</span> <span class="br0">&#123;</span>
            copy<span class="br0">&#91;</span>k<span class="br0">&#93;</span> <span class="sy0">=</span> deepCopy<span class="br0">&#40;</span>o<span class="br0">&#91;</span>k<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw1">return</span> copy<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>This will handle sparse arrays and cases of arrays treated as objects (e.g. var a = []; a.dontDoThis = &#8216;you should use an Object&#8217;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/comment-page-1/#comment-10299</link>
		<dc:creator>James</dc:creator>
		<pubDate>Thu, 23 Apr 2009 07:08:56 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=771#comment-10299</guid>
		<description>@Strx, functions are quite tricky. Like regular objects, functions exist in memory and when you assign one to a variable you&#039;re giving it a pointer, and every time you assign a function to a new variable all you&#039;re doing is  creating a new pointer to the same function.

Functions generally don&#039;t contain data or anything unique for that matter, so there&#039;s nothing to copy; their functionality cannot be changed in any way once the function has been created. So you can essentially copy a function (although not really &quot;copy&quot;) just by re-assigning it to a different variable:

&lt;pre lang=&quot;javascript&quot;&gt;
var fn = function(){
    alert(5);
};

var fn2 = fn;
// Even though fn === fn2 it doesn&#039;t matter because
// a function cannot be changed.
// All you can do with a function is run it.
&lt;/pre&gt;

But note that, because functions are objects, unique properties can be added and if that happens then you have to treat it like a regular object when copying.</description>
		<content:encoded><![CDATA[<p>@Strx, functions are quite tricky. Like regular objects, functions exist in memory and when you assign one to a variable you&#8217;re giving it a pointer, and every time you assign a function to a new variable all you&#8217;re doing is  creating a new pointer to the same function.</p>
<p>Functions generally don&#8217;t contain data or anything unique for that matter, so there&#8217;s nothing to copy; their functionality cannot be changed in any way once the function has been created. So you can essentially copy a function (although not really &#8220;copy&#8221;) just by re-assigning it to a different variable:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> fn <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    <span class="kw3">alert</span><span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="kw2">var</span> fn2 <span class="sy0">=</span> fn<span class="sy0">;</span>
<span class="co1">// Even though fn === fn2 it doesn't matter because</span>
<span class="co1">// a function cannot be changed.</span>
<span class="co1">// All you can do with a function is run it.</span></pre></div></div>

<p>But note that, because functions are objects, unique properties can be added and if that happens then you have to treat it like a regular object when copying.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Strx</title>
		<link>http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/comment-page-1/#comment-10297</link>
		<dc:creator>Strx</dc:creator>
		<pubDate>Thu, 23 Apr 2009 06:20:28 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=771#comment-10297</guid>
		<description>In the article you refer to Functions as complex types, but in the deepCopy they are not referenced. Why? How are functions copied?

Thanks for your posts, always useful.</description>
		<content:encoded><![CDATA[<p>In the article you refer to Functions as complex types, but in the deepCopy they are not referenced. Why? How are functions copied?</p>
<p>Thanks for your posts, always useful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/comment-page-1/#comment-10273</link>
		<dc:creator>James</dc:creator>
		<pubDate>Wed, 22 Apr 2009 16:04:52 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=771#comment-10273</guid>
		<description>@Ben, in some respects JavaScript does pass by value; I think there&#039;s a lot of confusion; I thought it was pass-by-reference but apparently calling it that is misleading: http://javadude.com/articles/passbyvalue.htm (Even though the article is about Java it applies to JavaScript). If you try &quot;The Litmus Test&quot; in JavaScript you&#039;ll see it doesn&#039;t pass. The author claims it&#039;s better to say that &quot;Object references are passed by value&quot; instead of &quot;Objects are passed by reference&quot;. 

Thanks for your comment! :)</description>
		<content:encoded><![CDATA[<p>@Ben, in some respects JavaScript does pass by value; I think there&#8217;s a lot of confusion; I thought it was pass-by-reference but apparently calling it that is misleading: <a href="http://javadude.com/articles/passbyvalue.htm">http://javadude.com/articles/passbyvalue.htm</a> (Even though the article is about Java it applies to JavaScript). If you try &#8220;The Litmus Test&#8221; in JavaScript you&#8217;ll see it doesn&#8217;t pass. The author claims it&#8217;s better to say that &#8220;Object references are passed by value&#8221; instead of &#8220;Objects are passed by reference&#8221;. </p>
<p>Thanks for your comment! <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Nadel</title>
		<link>http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/comment-page-1/#comment-10267</link>
		<dc:creator>Ben Nadel</dc:creator>
		<pubDate>Wed, 22 Apr 2009 14:18:12 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=771#comment-10267</guid>
		<description>I&#039;ve been coding in ColdFusion for so long, I totally forgot that arrays are passed by reference in Javascript. ColdFusion passes them by value. Thanks for the reminder and the cool post!

On a side note, I&#039;ve noticed that you always use arguments.callee rather than ever referring to the name of the function. I like your style - low coupling. Good stuff.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been coding in ColdFusion for so long, I totally forgot that arrays are passed by reference in Javascript. ColdFusion passes them by value. Thanks for the reminder and the cool post!</p>
<p>On a side note, I&#8217;ve noticed that you always use arguments.callee rather than ever referring to the name of the function. I like your style &#8211; low coupling. Good stuff.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
