I wrote a javascript function to extend an object with a property(s) that follow this sort of notation: "a.b.c". The function works flawlessly (so far) but I couldn't quite understand after looking back over my code why it worked. I'm assuming there is a javascript scope explanation, maybe the for loop? but I was wondering if anyone could explain this to me properly.
var extend = (target, targetProperty, value) => {
  var targetProperties = targetProperty.split(".");
  for (var i = 0, len = targetProperties.length; i < len; i++) {
    var part = targetProperties[i];
    if (i == len - 1) {
      target[part] = value;
      break;
    }
    if (!target[part]) {
      target[part] = {};
    }
    target = target[part];
  }
}
var myObject = {myProperty: "Hello"};
console.log(myObject);
extend(myObject, "a.b.c", "World");
console.log(myObject);
It is pretty straight forward, I understand that "target" is coming in by reference because it is an object, the part that I don't understand is how on the line target = target[part]; the original object being passed in is not completely overwritten by the newly created empty object on this line target[part] = {};.
Thanks!
 
     
    