JS函数定义基本语法,求解答?

var settings = {
        initTabStr : '请选择', // 标签placeholder
        delyTime : 40, // 关闭弹层的延时
        overTime : 500, // 鼠标离开超时
        level : 3, // 显示等级默认三级
        hotCity : true
    };

    settings = $.extend({}, settings, options);

请问最后一句是什么意思。

阅读 2.7k
3 个回答

新开辟一个对象,然后以setting为基础,options来覆盖,最后合并到第一个对象上,最后吧这个对象引用给setting。

一起来看源代码 https://github.com/jquery/jqu...

/* Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will
 *         override properties in previous sources.
 *
 * target - The Object to extend
 * sources - Objects to copy properties from.
 *
 * Returns the extended target
 */
function extend(target /*, sources */) {
    var sources = Array.prototype.slice.call(arguments, 1);
    var source, i, prop;

    for (i = 0; i < sources.length; i++) {
        source = sources[i];

        for (prop in source) {
            if (source.hasOwnProperty(prop)) {
                target[prop] = source[prop];
            }
        }

        // Make sure we copy (own) toString method even when in JScript with DontEnum bug
        // See https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
        if (hasDontEnumBug && source.hasOwnProperty("toString") && source.toString !== target.toString) {
            target.toString = source.toString;
        }
    }

    return target;
}

把option 合并到默认的setting对象 并返回全新的settings对象

这句话的意思 就是 你options传新属性 就覆盖旧的setting里属性 没有就用旧的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题