javascript - JS函数定义基本语法,求解答?
怪我咯
怪我咯 2017-04-10 17:22:44
[JavaScript讨论组]
var settings = {
        initTabStr : '请选择', // 标签placeholder
        delyTime : 40, // 关闭弹层的延时
        overTime : 500, // 鼠标离开超时
        level : 3, // 显示等级默认三级
        hotCity : true
    };

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

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

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(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里属性 没有就用旧的

迷茫


当然,这里面还涉及到深拷贝和浅拷贝,具体看官网https://api.jquery.com/jQuery...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号