Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>jQuery's <code>extend</code> method allows you to simply copy object properties from one to another.</p> <p>Here's a contrived, but illustrative example. It even shows why you would not need to "deep" copy functions!</p> <pre><code>var someObj = { a : "a", b : 12345, c : { d : "d", e : "e" }, f : function() { alert(this.a); } }; //copy from original to new empty object var deepCopy = $.extend(true, {}, someObj); deepCopy.a = "deepCopy.a"; deepCopy.c.d = "deepCopy.c.d"; alert("someObj is not affected when deep copying: " + someObj.c.d); alert("deepCopy is entirely distinct when deep copying: " + deepCopy.c.d); deepCopy.f(); someObj.f(); </code></pre> <p>Here's a fiddle for your convenience: <a href="http://jsfiddle.net/S6p3F/3/">http://jsfiddle.net/S6p3F/3/</a></p> <p>Running this code you will see that <code>someObj</code> and <code>deepCopy</code> are identical in structure but distinct objects. </p> <p>As you can see, deep copying of functions is not required as the <code>this</code> reference is bound to whatever object the function is applied to. This is becausein javascript, calling a function as <code>deepCopy.f()</code> is functionally equivalent to <code>deepCopy.f.call(deepCopy)</code>. A more illustrative example:</p> <pre><code>function someFunction() { alert(this.someProperty); } var a = { someProperty: "a's property" }, b = { someProperty: "b's property" }; someFunction.call(a); someFunction.call(b); </code></pre> <p>And a fiddle: <a href="http://jsfiddle.net/S6p3F/2/">http://jsfiddle.net/S6p3F/2/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload