millermedeiros / amd-utils

modular JavaScript utilities written in the AMD format
http://millermedeiros.github.com/amd-utils
142 stars 11 forks source link

object/mixIn short-circuits on falsey arguments #92

Closed JamesMGreene closed 12 years ago

JamesMGreene commented 12 years ago

Regarding: https://github.com/millermedeiros/amd-utils/blob/master/src/object/mixIn.js

Your mixIn implementation short-circuits on falsey arguments, which I doubt was intentional.

For example, in the following code, the target object will not contain a username property because the preceding argument being null short-circuited the copying loop:

mixIn({ "name": "James" }, { "surname": "Greene" }, null, { "username": "JamesMGreene" });

Patched version:

define(['./forOwn'], function(forOwn){

    function mixIn(target, objects){
        var i = 1,
            len = arguments.length,
            obj;
        while(i < len){
            if (obj = arguments[i++]){
                forOwn(obj, copyProp, target);
            }
        }
        return target;
    }

    function copyProp(val, key){
        this[key] = val;
    }

    return mixIn;
});