title: jQuery Extend copying over previous closures
body: <p><strong>Description</strong></p>
  
  <p>In my example I am trying to define a simple class (called <code>Car</code>) with  a <code>private</code> member and a <code>public</code> getter. I also have another object that I would like to to map into an instance of my class. I am using <code>jQuery.extend()</code> to achieve this and it works well except if the member is private (not tied to this). I am guessing this is because the getter function has closed over the values defined at the object creation time.</p>
  
  <p>I would still like to be able to re-close the function over the member after the <code>.extend()</code> so it will reflect the new value, can any one help me achieve this?</p>
  
  <p>I have also tried extending the passed in reference to a {} object and then using that blank object as the target like so:</p>
  
  <pre><code>function mapToObject(template, json) {
      var b = $.extend(true,{}, template);
    return $.extend(true, b, json);
  }
  </code></pre>
  
  <p>This also doesnt break the previous closure.</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>// simple class with private member
  function Car() {
    var noOfWheels = 2;
    this.honk = function() {
      alert(noOfWheels);
    }
  }
  // method which converts JSON object to a more typed class object
  function mapToObject(template, json) {
    return $.extend(true, template, json);
  }
  
  // sample Object , in reality coming from AJAX response
  var t = {
    noOfWheels: 5
  };
  // i want to map t to an instance of Car
  var r = mapToObject(new Car, t);// closing here during new Car, i think
  r.honk(); // this alerts 2 not 5 because of closure!
  </code></pre>
  
  <p><strong>Fiddle:</strong></p>
  
  <p><a href="https://jsfiddle.net/sajjansarkar/samj3pjq/2/" rel="nofollow">https://jsfiddle.net/sajjansarkar/samj3pjq/2/</a></p>
  
  <p><strong>[EDIT]My Solution based on DrFlink's answer:</strong></p>
  
  <pre><code>// one class
  function Car(options) {
    var settings = $.extend({
      noOfWheels: 2
    }, options || {});
    this.honk = function() {
      alert(settings.noOfWheels);
    }
  }
  // another class
  function Lemon(options) {
    var settings = $.extend({
      color: null
    }, options || {});
    this.getColor = function() {
      alert(settings.color);
    }
  }
  // function to map any json to a template class instance
  function ObjectMapperFactory(template, json) {
    if (!jQuery.isArray(json)) {
      return new template(json);
    } else {
      var returnArray = [];
      $(json).each(function() {
        returnArray.push(new template(this))
      });
      return returnArray;
    }
  }
  
  //test 1
  var carJSON = {
    noOfWheels: 5
  };
  var c = ObjectMapperFactory(Car, carJSON);
  c.honk();
  //test2 -different class and also testing array
  var lemonJSON = [{
    color: "yeller"
  },{
    color: "red"
  }];
  
  var c = ObjectMapperFactory(Lemon, lemonJSON);
  c[0].getColor();
  c[1].getColor();
  </code></pre>
  
  <p><strong>UPDATED FIDDLE:</strong></p>
  
  <p><a href="https://jsfiddle.net/sajjansarkar/samj3pjq/3/" rel="nofollow">https://jsfiddle.net/sajjansarkar/samj3pjq/3/</a></p>