Extending an object by reference
<p>I have an <code>extend</code> method that extends objects like you would expect it would in JavaScript:</p>

<pre><code>var objA = {a: true};
var objB = extend({b:false}, objA);
console.log(objB); // {a: true, b: false}
</code></pre>

<p>However, I would like to extend the source's (<code>objA</code>) properties onto the target (<code>objB</code>) object by reference so that any changes made to the source are reflected in the target after the fact, like so:</p>

<pre><code>var objA = {a: true};
var objB = extend({b: false}, objA);
console.log(objB); // {a: true, b:false}
objA.a = false;
console.log(objB); // {a: false, b:false}
</code></pre>

<p>For instance, when you modify an object (which is always assigned by reference) things are working the way I would like them to:</p>

<pre><code>var objA = {A:{a: true}};
var objB = _.extend({b: false}, objA);
console.log(objB) // {A:{a:true}, b:false};
objA.A.a = false; 
console.log(objB); // {A:{a:false}, b:false};
</code></pre>

<p>So in other words, when changes are made to <code>objA</code>'s non-object literal properties (or any value that isn't assigned by reference) I would like those changes to be reflected in <code>objB</code>.</p>

<p>I'm fairly sure this isn't exactly possible without an additional helper method that would depend on some sort of object watch method that is triggered whenever an object changes.</p>

<p>Thoughts?</p>

<p>Code from my <code>extend</code> method:</p>

<pre><code>(l).extend = (l).merge = function () {
    var args = (l).fn.__args(arguments, [{
            deep: 'bool'
        }, {
            '*': 'obj:object'
        }
    ]),
        target = (l)._object || args.obj,
        keys = [],
        obj, objs, copy, key, i, o;

    // Collect potential objects to merge
    objs = (l).filter(args, function (value, index) {
        if (index !== "obj" &amp;&amp;
            (l).isPlainObject(value) &amp;&amp; !((l).isEqual(target, value))) {
            return value;
        }
    });

    // When target object is not selected globally
    if (!args.obj) {
        target = objs.shift();
    }

    // When target object is not selected globally and only a single object
    // is passed extend the library itself
    if (!(l)._global &amp;&amp; !objs.length) {
        target = this;
        objs[0] = args.obj;
    }

    // When a boolean is passed go deep
    if (args.deep) {

        // Build property reference used to prevent never ending loops
        (l).each(objs, function (index, value) {
            keys.push((l).keys(value));
            keys = (l).flatten(keys);
        });

        // Add properties to all nested objects
        (l).deep(target, function (depth, index, obj, ref) {
            if ((l).indexOf(keys, index) === -1) {
                for (i = 0; i &lt; objs.length; i++) {
                    for (key in objs[i]) {
                        if ((l).isPlainObject(obj)) {
                            copy = objs[i][key];
                            obj[key] = copy;
                        }
                    }
                }
            }
        }, "*");
    }

    // Merge first level properties after going deep
    for (i = 0; i &lt; objs.length; i++) {
        if ((obj = objs[i]) !== null) {
            for (key in obj) {
                copy = obj[key];
                if (target === copy) {
                    continue;
                }
                target[key] = copy;
            }
        }
    }
    return (l).fn.__chain(target);
};
</code></pre>
