JavaScript: How to pass object by value?
<ul>
<li><p>When passing objects as parameters, JavaScript passes them by reference and makes it hard to create local copies of the objects.</p>

<pre><code>var o = {};
(function(x){
    var obj = x;
    obj.foo = 'foo';
    obj.bar = 'bar';
})(o)
</code></pre>

<p><em><strong><code>o</code> will have <code>.foo</code> and <code>.bar</code>.</em></strong></p></li>
<li><p>It's possible to get around this by cloning; simple example:</p>

<pre><code>var o = {};

function Clone(x) {
   for(p in x)
   this[p] = (typeof(x[p]) == 'object')? new Clone(x[p]) : x[p];
}

(function(x){
    var obj = new Clone(x);
    obj.foo = 'foo';
    obj.bar = 'bar';
})(o)
</code></pre>

<p><em><strong><code>o</code> will not have <code>.foo</code> or <code>.bar</code>.</em></strong></p></li>
</ul>

<hr>

<h3>Question</h3>

<ol>
<li>Is there a better way to pass objects by value, other than creating a local copy/clone?</li>
</ol>
