title: How can I dynamically create objects by a given string name?
body: <p>I am trying to create object from given string. The string value should be the name of the new object. I created a root object and the create the subsequent objects by give in a string name and clone from the root object.</p>
  
  <p>I am using jointJs.</p>
  
  <p>Here is the root object</p>
  
  <pre><code>var lvl0 = new joint.shapes.basic.Rect({
      position: { x: 100, y: 30 },
      size: { width: 100, height: 30 },
      attrs: { rect: { fill: '#31d0c6', stroke: 'none',  filter: { name: 'dropShadow',  args: { dx: 3, dy: 6, blur: 3, color: '#333333' }} }, 
               text: { text: 'lvl0', fill: '#ffffff' }, 
               style: { 'text-shadow': '1px 0 1px #333333' }  }
  
  });
  </code></pre>
  
  <p>Then the function that clones the root object and assign a given name to the new object.</p>
  
  <pre><code>function add_cell(parent, child, lvl, color) {
    if (lvl === 0)
    { var child = parent.clone().translate(0, 0).attr('text/text', child);  }
    else { var child = parent.clone().translate(200, 0).attr('text/text', child).attr('rect/fill', color); }
    graph.addCell(child);
    if (lvl != 0) {  createLink(parent, child).set(createLabel('0,N     1,1')); }
  }
  </code></pre>
  
  <p>Here is the calling code.</p>
  
  <pre><code>var lst = [ { "parent": "lvl0", "child": "dept", "lvl": 0, "color": "green" }, { "parent": "dept", "child": "empl", "lvl": 1, "color": "yellow" } ];
  for (var i = 0; i &lt; lst.length; i++) {
    add_cell (window[lst[i].parent], lst[i].child, lst[i].lvl, lst[i].color);
  }
  </code></pre>
  
  <p>The first clone is successful because the object <strong>lvl0</strong> being clone is an existing object. But the second clone will fail because object <strong>dept</strong> does not exist. Instead the <strong>child</strong> variable is overwritten to a cloned object. I basically need to rename the cloned object from <strong>child</strong> to <strong>dept</strong>. How can I do this?</p>
  
  <p>Thanks,</p>