$ .extend的一个变体,用于保存具有未定义值的属性?

时间:2015-10-08 13:23:42

标签: javascript jquery undefined

我看到这段代码:

$.extend(true, {}, { data: undefined });

返回一个空对象:{}。我在Chrome 45.0.2454.101(64位)上使用jQuery 2.1.1对此进行了测试。

$.extend的变体是否会保留具有undefined值的属性?你能举个例子吗?

非常感谢! :-)

2 个答案:

答案 0 :(得分:0)

使用Jquery extend方法无法做到这一点。 API文档https://api.jquery.com/jquery.extend/

中明确提到了这一点
  

当两个或多个对象参数提供给$ .extend()时,   所有对象的属性都将添加到目标对象中。   忽略null或undefined的参数。

答案 1 :(得分:0)

正如问题的评论中所写,jQuery文档提到jQuery实现忽略了undefined值的属性。在我的代码中,我不想使用其他库,例如lodash,所以我从jQuery' s $.extend中获取了代码,然后我对其进行了一些更改,以保留属性为undefined的值

/**
 * extend
 * A jQuery $.extend implementation changed to keep properties with
 * undefined values. The original source code was taken from here:
 * https://github.com/jquery/jquery/blob/64f7d10980a5e9f2862f1239a37d95e6c39e37ec/src/core.js
 * and the original documentation was taken from here:
 * http://api.jquery.com/jQuery.extend/
 *
 * Merge the contents of two or more objects together into the first object.
 *
 * @name extend
 * @function
 * @param {Boolean} deep If true, the merge becomes recursive (aka. deep copy).
 * @param {Object} target The object to extend. It will receive the new properties.
 * @param {Object} object1 An object containing additional properties to merge in.
 * @param {Object} objectN Additional objects containing properties to merge in.
 * @return {Object} The modified target object.
 */
function extend() {
    var options, name, src, copy, copyIsArray, clone,
        target = arguments[0] || {},
        i = 1,
        length = arguments.length,
        deep = false;

    // Handle a deep copy situation
    if (typeof target === "boolean") {
        deep = target;

        // Skip the boolean and the target
        target = arguments[i] || {};
        i++;
    }

    // Handle case when target is a string or something (possible in deep copy)
    if (typeof target !== "object" && !$.isFunction(target)) {
        target = {};
    }

    // Extend jQuery itself if only one argument is passed
    if (i === length) {
        target = this;
        i--;
    }

    for (; i < length; i++) {
        // Only deal with non-null/undefined values
        if ((options = arguments[i]) != null) {
            // Extend the base object
            for (name in options) {
                src = target[name];
                copy = options[name];

                // Prevent never-ending loop
                if (target === copy) {
                    continue;
                }

                // Recurse if we're merging plain objects or arrays
                if (deep && copy && ($.isPlainObject(copy) || (copyIsArray = $.isArray(copy)))) {
                    if (copyIsArray) {
                        copyIsArray = false;
                        clone = src && $.isArray(src) ? src : [];

                    } else {
                        clone = src && $.isPlainObject(src) ? src : {};
                    }

                    // Never move original objects, clone them
                    target[name] = jQuery.extend(deep, clone, copy);

                // Modified this else branch to allow undefined values
                } else {
                    target[name] = copy;
                }
            }
        }
    }

    // Return the modified object
    return target;
}