Skip to content

Instantly share code, notes, and snippets.

@adamvr
Created September 19, 2013 09:21
Show Gist options
  • Select an option

  • Save adamvr/6621065 to your computer and use it in GitHub Desktop.

Select an option

Save adamvr/6621065 to your computer and use it in GitHub Desktop.

Revisions

  1. Adam Rudd created this gist Sep 19, 2013.
    35 changes: 35 additions & 0 deletions augment.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    var augment = function (obj /* args */) {
    var args = Array.prototype.slice.call(arguments, 1);

    for (var i = 0; i < args.length; i += 1) {
    var arg = args[i];
    // Skip non-objects
    if ('object' !== typeof arg) continue;

    for (var k in arg) {
    var v = arg[k];
    // Skip keys that are already present
    if (k in obj) continue;
    obj[k] = v;
    }
    }

    return obj;
    };

    var a = {
    a: 'b',
    c: 'd',
    e: 'f'
    };

    var b = {
    a: 'dontoverride',
    f: 'g',
    h: 'a'
    }

    var c = {
    f: 'dontoverride',
    e: 'dontoverride'
    }