Tuesday, June 11, 2013

shallow copies in both jQuery and Dojo

This had the following image today which suggested that shallow copying in JavaScript entails creating a new pointer to an object or array that already has a pointer as opposed to making a deep copy in which an object (or array I suppose), its contents, and its pointer are all cloned elsewhere to an independent thing that would be unaware of alterations to the original after the point in time of the copy.

Here is a Dojo 1.9 example:

function shallowCopy(from, to) {
   require(["dojo/_base/lang"], function(lang){
      foo[to] = lang.mixin({},foo[from]);
   });
}

 
 

jQuery Shallow copy

var newObject = jQuery.extend({}, oldObject);

 
 

jQuery Deep copy

var newObject = jQuery.extend(true, {}, oldObject);

No comments:

Post a Comment